This is a straight C++ question; not DEV related. But my brain hurts!
This code compiles but has one major flaw: -
------------------
#include <string>
typedef class Base{
protected:
int Action,
Target;
private:
void splitMsg(const int m);
public:
Base();
~Base();
virtual int msg(const int m,int& p) = 0; //make abstract
};
typedef class Derived1:public Base{
public:
Derived1();
~Derived1();
int msg(const int m,int& p);
virtual int msg(const int m,float& p);
};
typedef class Derived2:public Base{
public:
Derived2();
~Derived2();
int msg(const int m,int& p);
virtual int msg(const int m,string& p);
};
typedef class Derived3:public Base{
public:
Derived3();
~Derived3();
int msg(const int m,int& p);
virtual int msg(const int m,string& p);
virtual int msg(const int m,Derived2& p);
};
-------------------
the overloaded versions of msg() are not accessable from a pointer to the base class.
This solves it, but:-
-------------------
#include <string>
typedef class Base{
protected:
int Action,
Target;
private:
void splitMsg(const int m);
public:
Base();
~Base();
virtual int msg(const int m,int& p) = 0; //make abstrac
virtual int msg(const int m,float& p);
virtual int msg(const int m,string& p);
virtual int msg(const int m,Base& p);
};
typedef class Derived1:public Base{
public:
Derived1();
~Derived1();
int msg(const int m,int& p);
int msg(const int m,float& p);
};
typedef class Derived2:public Base{
public:
Derived2();
~Derived2();
int msg(const int m,int& p);
int msg(const int m,string& p);
};
typedef class Derived3:public Base{
public:
Derived3();
~Derived3();
int msg(const int m,int& p);
int msg(const int m,string& p);
int msg(const int m,Derived2& p);
};
-----------------
I'm already looking at about 50 derived classes so it's going to get messy.
can I do somthing like this
-----------
typedef class Base{
protected:
int Action,
Target;
private:
void splitMsg(const int m);
public:
Base();
~Base();
template<typenameT>
int msg(const int m,T& p) = 0; //make abstrac
};
-------------------
and if so, how do I declare the derived classes and define their functions.
Also, in the second block of code, is the
int Derived3:: msg(const int m,Derived2& p)
function base on the virtual function in the Base class, or is it overloaded becase Base* and Derived2* are not the same??
Any help would be greatly appreciated.
Light Knight
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
"In Chapter 14 you saw how an object can be used as its own type or as an object of its base type. In addition, it can be manipulated through an address of the base type. Taking the address of an object (either a pointer or a reference) and treating it as the address of the base type is called upcasting because of the way inheritance trees are drawn with the base class at the top."
(Thinking in C++ vol 1)
Not that I understand what it says, but I wonder.
rpeter
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Your right, the ability to declare an object of the base type within a derived class is called upcasting. But you can't do the opposite ie. refer to a derived type within the base, because it does not exist yet.
And
int Derived3:: msg(const int m,Derived2& p)
is overloaded, so I'd have to change it to
int Derived3:: msg(const int m,Base& p)
to access it via a Base*. This is only Ok up to a point. I'd have to be careful because I wouldn't be able to rely on the compiler for type checking, it'd accept a pointer to any derived class - not good!
I'm looking a the template idea inside out too. I think the proper aproach is to 'make' the base class a 'template' etc. etc.
Thanks rpeter, but I think I'll just have to keep experimenting until I get it right or headbutt the screen!
LK
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This is a straight C++ question; not DEV related. But my brain hurts!
This code compiles but has one major flaw: -
------------------
#include <string>
typedef class Base{
protected:
int Action,
Target;
private:
void splitMsg(const int m);
public:
Base();
~Base();
virtual int msg(const int m,int& p) = 0; //make abstract
};
typedef class Derived1:public Base{
public:
Derived1();
~Derived1();
int msg(const int m,int& p);
virtual int msg(const int m,float& p);
};
typedef class Derived2:public Base{
public:
Derived2();
~Derived2();
int msg(const int m,int& p);
virtual int msg(const int m,string& p);
};
typedef class Derived3:public Base{
public:
Derived3();
~Derived3();
int msg(const int m,int& p);
virtual int msg(const int m,string& p);
virtual int msg(const int m,Derived2& p);
};
-------------------
the overloaded versions of msg() are not accessable from a pointer to the base class.
This solves it, but:-
-------------------
#include <string>
typedef class Base{
protected:
int Action,
Target;
private:
void splitMsg(const int m);
public:
Base();
~Base();
virtual int msg(const int m,int& p) = 0; //make abstrac
virtual int msg(const int m,float& p);
virtual int msg(const int m,string& p);
virtual int msg(const int m,Base& p);
};
typedef class Derived1:public Base{
public:
Derived1();
~Derived1();
int msg(const int m,int& p);
int msg(const int m,float& p);
};
typedef class Derived2:public Base{
public:
Derived2();
~Derived2();
int msg(const int m,int& p);
int msg(const int m,string& p);
};
typedef class Derived3:public Base{
public:
Derived3();
~Derived3();
int msg(const int m,int& p);
int msg(const int m,string& p);
int msg(const int m,Derived2& p);
};
-----------------
I'm already looking at about 50 derived classes so it's going to get messy.
can I do somthing like this
-----------
typedef class Base{
protected:
int Action,
Target;
private:
void splitMsg(const int m);
public:
Base();
~Base();
template<typenameT>
int msg(const int m,T& p) = 0; //make abstrac
};
-------------------
and if so, how do I declare the derived classes and define their functions.
Also, in the second block of code, is the
int Derived3:: msg(const int m,Derived2& p)
function base on the virtual function in the Base class, or is it overloaded becase Base* and Derived2* are not the same??
Any help would be greatly appreciated.
Light Knight
"In Chapter 14 you saw how an object can be used as its own type or as an object of its base type. In addition, it can be manipulated through an address of the base type. Taking the address of an object (either a pointer or a reference) and treating it as the address of the base type is called upcasting because of the way inheritance trees are drawn with the base class at the top."
(Thinking in C++ vol 1)
Not that I understand what it says, but I wonder.
rpeter
Your right, the ability to declare an object of the base type within a derived class is called upcasting. But you can't do the opposite ie. refer to a derived type within the base, because it does not exist yet.
And
int Derived3:: msg(const int m,Derived2& p)
is overloaded, so I'd have to change it to
int Derived3:: msg(const int m,Base& p)
to access it via a Base*. This is only Ok up to a point. I'd have to be careful because I wouldn't be able to rely on the compiler for type checking, it'd accept a pointer to any derived class - not good!
I'm looking a the template idea inside out too. I think the proper aproach is to 'make' the base class a 'template' etc. etc.
Thanks rpeter, but I think I'll just have to keep experimenting until I get it right or headbutt the screen!
LK