Skip to content

Instantly share code, notes, and snippets.

@drodil
Last active October 3, 2018 05:25
Show Gist options
  • Save drodil/cb0403f5ead452ff77b0138d5da7dcec to your computer and use it in GitHub Desktop.
Save drodil/cb0403f5ead452ff77b0138d5da7dcec to your computer and use it in GitHub Desktop.
#include <iostream>
class HiMyNameIs {
public:
void SlimShady() { std::cout << "Hi! My name is (what?)" << std::endl; }
};
void HiMyNameIs() {
std::cout << "My name is who?" << std::endl;
}
struct NameHolder {
std::string name = "Slim Shady";
};
void NameHolder(const std::string& name) {
std::cout << name << std::endl;
}
int main() {
// Trying to initiate the class that matches with function name
// will not work and will cause the following compilation error:
// error: expected ';' before 'clazz'
// HiMyNameIs clazz;
// By adding 'class' keyword, the compiler knows we are meaning the
// class, not the function
class HiMyNameIs clazz;
clazz.SlimShady();
// Calling a function works just as it should
HiMyNameIs();
// Same thing for a struct; the following won't compile:
// NameHolder holder;
struct NameHolder holder;
NameHolder(holder.name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment