Recognizing Specific Types

This is a discussion on Recognizing Specific Types within the C++ Programming forums, part of the General Programming Boards category; Okay, let's imagine this code: Code: class CBase { public: void my_func (CBase &obj) { } int data; }; class ...

  1. #1
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Recognizing Specific Types

    Okay, let's imagine this code:

    Code:
    class CBase {
    public:
    	void  my_func (CBase &obj) {  }
    
    	int data;
    };
    
    class CDer_a : public CBase
    {
    public:
    	int data;
    };
    
    class CDer_b : public CDer_a
    {
    public:
    	float data;
    };
    Now, I have

    CBase ObjectA;
    CDer_a ObjectB;
    CDer_b ObjectC;

    within CBase::my_func() how can I tell what "type" is passed? So, if I call CBase::my_func() with ObjectB as a parameter, can I have some sort of if statement in my_func saying "it's of type CDer_a, do this, or no, it's of type CDer_b, do this"?

    Something to do with typeid? Thanks in advance.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Maybe you could use a type function. Something like a virtual function in the base class and then have it in the other classes. Just have the different implementations return different values, and from the return values, you could tell what it was.

    Code:
    class base
    { virtual int func(){return 1;};
    } 
    
    class derived: public base
    { int func(){return 2;};//and so on
    }

    There is probably an error there. I just threw it together for explanation purposes.

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Well , I mean without redefining the functions in derived classes, rather, having the function in CBase go different routes based on the "type" passed to it.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I think making my_func() a template function may allow you do what you wish. How template functions within a class will work out I don't know. I do know if you make a template class, then you need to use all the class definitions withing the header files, and not in a cpp file. Whether there are similar restrictions on using a template method would be the quetion.

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >can I have some sort of if statement in my_func saying "it's of type CDer_a, do this, or no, it's of type CDer_b, do this"?

    Is this not the point of virtual functions? You could do something with typeinfo (or even using a overrided string) to get info of the type -

    Code:
    #include <iostream>
    #include <typeinfo>
    
    using namespace std;
    
    
    class CBase {
    public:
    	virtual void  my_func () {
    		cout << typeid(*this).name() << '\n';
    	}
    
    	int data;
    };
    
    class CDer_a : public CBase
    {
    public:
    	int data;
    };
    
    class CDer_b : public CDer_a
    {
    public:
    	float data;
    };
    
    int main()
    {
    
    	CDer_a a;
    	a.my_func();
    	CBase b;
    	b.my_func();
    	
    
    	return 0;
    }
    However, this would be less efficient than using virtual functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generalising methods to handle multiple weapon types
    By Swarvy in forum Game Programming
    Replies: 2
    Last Post: 05-22-2009, 02:52 AM
  2. What types of files can C++ file processing handle?
    By darsunt in forum C++ Programming
    Replies: 9
    Last Post: 10-28-2008, 11:33 AM
  3. Data Types
    By drrcknlsn in forum C++ Programming
    Replies: 7
    Last Post: 01-17-2008, 02:52 AM
  4. platform specific API or C standard API
    By George2 in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 12:32 AM
  5. Specific Keys in C, getting and putting
    By Axpen in forum C Programming
    Replies: 17
    Last Post: 01-08-2004, 08:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21