Function that returns object name in c++

I want to initialise an object say A of class alphabet. the data for the object is located in a textfile A.txt.

How can I write a function in c++ that returns the name of the object as a char[] or string that I can manipulate into a filename, i.e. return char filename[]=A if the Object name is A.

How would this be possible also for Object arrays?
You can't just "get" a variable/class/structs name, you will probably have to add a variable that stores its name inside the class/struct.
firedraco is right, my first thought is that since you can initialize strings in c++ as such:

char szClassName[] = "CustomObjectClassName";

...put the preceding line of code in the "whateveryourclassis.c" file, but out of the scope of the class itself. this way, other files can't access it, but you can put a method in the ".c" file similar to this:

1
2
3
4
5
6
7
// pretend this is your header file
class customClass
{
public:
char* getClassNameString(void);
}
//____end header file________________ 

1
2
3
4
5
6
7
8
9
10
11
// this is your ".c" file
#include <yourheader.h>

char szClassName[] = "customClass\0";

char* customClass::getClassNameString(void)
{
return szClassName;
}

//______end .c file__________________ 


...i shouldn't, but i forget if the '\0' at the end is necessary to make it null terminated, i don't believe it is, but i'm not sure. I should know, but i also believe that by just returning szClassName that it will actually return a pointer to the first character in the string, but again, it's been a little while, so you might want to check.
You can use rtti (run-time type information), namely typeid(x).name(), where x is an object of the type in question. However, the output is implementation-defined and is most likely tangled (C++ implementations use tangling to make unique function names from sets of overloaded functions and functions of the same name contained in different classes/namespaces). So to untangle them you have to read the documentation of your compiler. Also note that, following from that, this is non-portable, so perhaps the solution suggested above is better if at all applicable.
If your compiler supports the __PRETTY_FUNCTION__ macro, then you can do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

template< typename Derived >
struct NamedObject {
    NamedObject() : objName( __PRETTY_FUNCTION__ ) {}
    std::string ObjectName() const { return objName; }
  private:
    std::string objName;
};

class D: public NamedObject<D> {};

int main() {
    D d;
    std::cout << d.ObjectName() << std::endl;
}


Outputs on my machine:

NamedObject<Derived>::NamedObject() [with Derived = D]

Just need to parse out the text after the equal sign.


Wow, I never heard about that macro before... Do you know how wide-spread it's implementation is?
I think it is GCC-specific (which is why I started my statement "If your compiler supports..."). :)
Thanks for the advice but i guess my compiler cant handle. this is the result:
1>------ Build started: Project: HBV_Jjunju, Configuration: Debug Win32 ------
1>Compiling...
1>objectname.cpp
1>c:\hbv_jjunju\hbv_jjunju\objectname.cpp(6) : error C2065: '__PRETTY_FUNCTION__' : undeclared identifier
1> c:\hbv_jjunju\hbv_jjunju\objectname.cpp(6) : while compiling class template member function 'NamedObject<Derived>::NamedObject(void)'
1> with
1> [
1> Derived=D
1> ]
1> c:\hbv_jjunju\hbv_jjunju\objectname.cpp(12) : see reference to class template instantiation 'NamedObject<Derived>' being compiled
1> with
1> [
1> Derived=D
1> ]
1>Build log was saved at "file://c:\HBV_Jjunju\HBV_Jjunju\Debug\BuildLog.htm"
1>HBV_Jjunju - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

No, because you are using a microsoft compiler.

So you'll either have to look a msoft documentation and see if they have an equivalent or use exception's idea of RTTI (the name will be mangled in this case).

will surely try again. thanks
Topic archived. No new replies allowed.