Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

like to do following. I want to derive multiple classes out of my base class.
All of them have there own function called notify. But i do not like to have any kind of
case distinction in the main() but just call a base function to make that and call the appropriate funtion from any derived one.

It could be 100s of derived classes and i don't like to write a huge if ... else or switch case pattern just to call the right methode and i also do not like to have 100 of derived instances in the main. I'm lazy and i thaught that c++ could somehow manage that in the backround. Maybe the derived classes could have a specific id which is const or would be delivered when instantiated. But how to invoke the right methode in the base class. No clue how to figur that out.


Thanks all for reply

What I have tried:

typedef struct {
   int id;
   int length;
   int payload[];
}data_t;

class base{
public:
   void fn(data_t data) {

   //here it should decide which of those function in derived classes it should magically 
   //call depending on the data.id value is there

   }
}

class derived1 : public base
{
public:
    const int id = 1;
    void notify() {...};
}

...

class derived100 : public base
{
public:
    const int id = 100;
    void notify() {...};
}

int main()
{
   base b;
   data_t data;

   //data struct will be filled here

   b.fn(data);

   return 0;
}
Posted
Updated 18-Oct-21 8:30am
v2
Comments
Richard MacCutchan 17-Oct-21 11:14am    
You cannot do it like that. The base class has no knowledge of its derived types, so it cannot access their members.

Define notify as virtual in base and override it in derived1 and derived2:
C++
class base
{
   public: virtual void notify() { }
}

class derived1 : public base
{
public: void notify() override {...};
}

class derived2 : public base
{
public: void notify() override {...};
}

int main()
{
   base* b = ...;  // create an instance of base, derived1, or derived2
   b->notify();    // invokes notify() in b's actual class
   return 0;
}
If that's not what you want, you'll have to be more specific.
 
Share this answer
 
v2
Comments
CPallini 18-Oct-21 2:11am    
5.
You can also make your class abstract which will require that derivations of it are implemented and it will prevent the base class itself from being instantiated. Here is how that might look :
C++
class BaseClass
{
public:
	void fn(int i)
	{
		notify( i );
	}

	virtual void notify( int i ) = 0;   // derived class must implement this
};

class Derived1 : public BaseClass
{
public:
	virtual void notify( int i )
	{
		printf( "notification 1 : value is %d\n", i );
	}
};

class Derived2 : public BaseClass
{
public:
	virtual void notify( int i )
	{
		printf( "notification 2 : value is %d\n", i );
	}
};
and this little testing function will compile and run :
C++
void TestDerivation()
{
	Derived1 d1;
	d1.fn( 42 );

	Derived2 d2;
	d2.fn( 24 );
}
 
Share this answer
 
v2
Comments
CPallini 18-Oct-21 2:11am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900