Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #3650

Re: Simple inheritance-template question

From "crea" <no@invalid.com>
Newsgroups comp.lang.c++
References <f5zpp.27551$rF.22899@newsfe12.ams2>
Subject Re: Simple inheritance-template question
Message-ID <6Azpp.30060$5t2.28736@newsfe06.ams2> (permalink)
Organization virginmedia.com
Date 2011-04-14 10:59 +0100

Show all headers | View raw


> Point is that for BB I want a data member of type "string" and for AA I 
> want a data member of type "int". And in both cases the data member name 
> is "m_data" and it is located in the class AA.

So it would look something like this:

template <class T> class Data
{
public:
 T p;
};

class AA
{
public:
 m_data
};

class BB : public AA
{
public:
};

If I create an object from BB:

BB b;

then m_data is created to be string-type (Data class member p becomes 
string)

And if I create an object from AA

AA b;

then m_data is created to be int-type (Data class member p becomes int).

Not sure if template is best to acquire this. (Actually in my real program 
its not int/string types, but classes where the other one is the parent of 
the other one. So I was first thinking of using inheritance +pointers 
instead of tamplate:

class Data
{
public:
 ParentClass* p;
};

and then just set p to point parent and/or its child class object, and 
virtuality would make sure they point to right classes.)




"crea" <no@invalid.com> wrote in message 
news:f5zpp.27551$rF.22899@newsfe12.ams2...
>I cannot find solution to this:
>
> Here is the structure:
>
> template <class T> class Data
> {
> public:
> T p;
> };
>
> class AA
> {
> public:
> // here I would like to have a data member: m_data
> };
>
> class BB : public AA
> {
> public:
> };
>
> If an object is create from AA then I want it to have member data object 
> created like this:
> Data<int> m_data;
>
> But if an object is created from BB then I want:
>
> Data<string> m_data;
>
> How to create a member data variable according to what class the object is 
> created from? Is this even possible?
>
>
>
>
>
> 

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Simple inheritance-template question "crea" <no@invalid.com> - 2011-04-14 10:26 +0100
  Re: Simple inheritance-template question Ian Collins <ian-news@hotmail.com> - 2011-04-14 21:51 +1200
    Re: Simple inheritance-template question "crea" <no@invalid.com> - 2011-04-14 11:12 +0100
      Re: Simple inheritance-template question "crea" <no@invalid.com> - 2011-04-14 11:29 +0100
      Re: Simple inheritance-template question Goran <goran.pusic@gmail.com> - 2011-04-14 03:56 -0700
        Re: Simple inheritance-template question "crea" <no@invalid.com> - 2011-04-14 12:19 +0100
          Re: Simple inheritance-template question Goran <goran.pusic@gmail.com> - 2011-04-14 04:58 -0700
            Re: Simple inheritance-template question "crea" <no@invalid.com> - 2011-04-14 13:25 +0100
            Re: Simple inheritance-template question "crea" <no@invalid.com> - 2011-04-14 13:38 +0100
              Re: Simple inheritance-template question "crea" <no@invalid.com> - 2011-04-14 13:48 +0100
                Re: Simple inheritance-template question Goran <goran.pusic@gmail.com> - 2011-04-14 06:06 -0700
    One template solution I know, but... "crea" <no@invalid.com> - 2011-04-14 11:41 +0100
      Re: One template solution I know, but... Victor Bazarov <v.bazarov@comcast.invalid> - 2011-04-14 09:32 -0400
        Re: One template solution I know, but... "crea" <no@invalid.com> - 2011-04-14 15:19 +0100
  Re: Simple inheritance-template question "crea" <no@invalid.com> - 2011-04-14 10:59 +0100
    Re: Simple inheritance-template question Noah Roberts <dont@email.me> - 2011-04-14 09:29 -0700
      Re: Simple inheritance-template question "crea" <no@invalid.com> - 2011-04-15 07:50 +0100
      Re: Simple inheritance-template question "crea" <no@invalid.com> - 2011-04-17 17:22 +0100

csiph-web