Last active
October 3, 2018 05:25
-
-
Save drodil/cb0403f5ead452ff77b0138d5da7dcec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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