2

I created class Class and I want the function PrintObjectName to print out the object name.

By calling "SomeObject.PrintObjectName();" I'm expecting that console will print "SomeObject". How to achieve this?

class Class
{
public:
    PrintObjectName()
    {
        cout<<OBJECTNAME;
    }
};

int main()
{
    Class SomeObject;
    SomeObject.PrintObjectName();

    return 0;
}```

Pobierac
  • 31
  • 2
  • 1
    You need to add a field into that class to hold object name (probably of `std::string` type) and fill it upon object creation. – user7860670 Jun 20 '19 at 12:21
  • Variable names are lost when the project is compiled. You can do that automatically. (You could achieve something close with macros though if you really wanted) – gan_ Jun 20 '19 at 12:22
  • Okay, I created string to hold it's name, but what do you mean by "filling it upon object creation"? I know that I can create parametrizer constructor who will take name as a parametr but this is uneffective because I would be forced to write down the name of an object twice (one as a name of object, and second as a parametr for constructor) – Pobierac Jun 20 '19 at 13:01

0 Answers0