object and class name reference

S

Salt_Peter

Gary said:
Hi

can I reference the class name from within the object?

thanks

yes, typeid()...

#include <iostream>
#include <typeinfo>

struct A {
virtual ~A() { }
};

struct B : public A {
};

int main()
{
B b;
A& r_a = b;

std::cout << "type of r_a: " << typeid(r_a).name() << std::endl;
}

Note: If you remove the virtual d~tor, you loose.
 
K

Kavya

Salt_Peter said:
yes, typeid()...

#include <iostream>
#include <typeinfo>

struct A {
virtual ~A() { }
};

struct B : public A {
};

int main()
{
B b;
A& r_a = b;

std::cout << "type of r_a: " << typeid(r_a).name() << std::endl;
}

Note: If you remove the virtual d~tor, you loose.

What do you mean by "if you remove virtual destructor you lose"?
 
S

Salt_Peter

Kavya said:
What do you mean by "if you remove virtual destructor you lose"?

I'ld kindly suggest trying it.
Without a virtual function in type A above, the class hierarchy would
no longer be polymorphic.
The typeid would identify the referenced object as type A erroneously.

Perhaps the following helps, same outcome:

int main()
{
B b;
A* const p_a = &b; // const pointer

std::cout << "type of r_a: " << typeid(*p_a).name() << std::endl;
}
 
K

Kai-Uwe Bux

Salt_Peter said:
yes, typeid()...

#include <iostream>
#include <typeinfo>

struct A {
virtual ~A() { }
};

struct B : public A {
};

int main()
{
B b;
A& r_a = b;

std::cout << "type of r_a: " << typeid(r_a).name() << std::endl;
}

On my machine, this prints:

type of r_a: 1B

Does not look exactly like the classname to me.

The standard would allow

#include <iostream>
#include <typeinfo>
struct Mickey_Mouse {};
int main() {
std::cout << typeid(Mickey_Mouse).name() << std::endl;
}

to print Donald_Duck.

[snip]


Best

Kai-Uwe Bux
 
D

Diwa

Did some research on this.
See inline.

Kai-Uwe Bux said:
On my machine, this prints:

type of r_a: 1B

Does not look exactly like the classname to me.

If on linux, do "c++filt 1B"
This will give unmangled name i.e. "B"

-- Diwakar
 
V

Victor Bazarov

Diwa said:
Did some research on this.
See inline.



If on linux, do "c++filt 1B"
This will give unmangled name i.e. "B"

Platform-specific information is off-topic.

The 'name' member function of 'type_info' class is NOT guaranteed to
return anything meaningful. It's all compiler-specific and off-topic.

V
 
D

Diwa

Victor said:
Platform-specific information is off-topic.

The 'name' member function of 'type_info' class is NOT guaranteed to
return anything meaningful. It's all compiler-specific and off-topic.
Yes, you are right and anyway "c++filt" is a cmd line program
and hence not usable in a C++ program even if its for Linux.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top