Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple inheritance-template question

Reply
Thread Tools

Simple inheritance-template question

 
 
crea
Guest
Posts: n/a
 
      04-14-2011

"Goran" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> but AA has like 5 different this kind of fucntions. Shall I just create a
> virtual function for all of them (5 of them)? I could obviously put them
> to
> be private. But is there any easier way... this is doable, but needs to
> override many functions.


"(Not private, protected, you need to override).

Overrides seem^^^ to be trivial: create a different object type given
some parameters, so why not?

(^^^: I am guessing that after new Data() you actually put something
useful in that object)."

I just checked, and actually the only place where I need some kind of
virtual calling is when I do this in couple of places:
m_Data.data.Add(new Data(...));

So the new -operation needs some kind of virtual version. hmmm , maybe just
to create a function NewObject:

in AA:

virtual Data NewObject()

{ return new Data(); }



and then have in BB:

virtual BBData NewObject()

{ return new BBData(); }



so the call in AA goes now:



m_Data.data.Add(NewObject());



What do you think!! Looks like perfect.


 
Reply With Quote
 
 
 
 
crea
Guest
Posts: n/a
 
      04-14-2011

"crea" <(E-Mail Removed)> wrote in message
news:dVBpp.546$(E-Mail Removed)2...
>
> virtual BBData NewObject()
>
> { return new BBData(); }
>


Only problem I see here is that the class derived from AA now has to
remember to create the NewObject fucntion. I they dont, it will fail. Is
there any way to force the child class to create always this NewObject
function, otherwise there is compile error? If not, there is a human error
possibility here if forgets to implement NewObject function in child
classes.


 
Reply With Quote
 
 
 
 
Goran
Guest
Posts: n/a
 
      04-14-2011
On Apr 14, 2:48*pm, "crea" <(E-Mail Removed)> wrote:
> "crea" <(E-Mail Removed)> wrote in message
>
> news:dVBpp.546$(E-Mail Removed)2...
>
>
>
> > virtual BBData NewObject()

>
> > { return new BBData(); }

>
> Only problem I see here is that the class derived from AA now has to
> remember to create the NewObject fucntion. I they dont, it will fail. Is
> there any way to force the child class to create always this NewObject
> function, otherwise there is compile error? If not, there is a human error
> possibility here if forgets to implement NewObject function in child
> classes.


Only if you forget to override __and__ upcast in derived class.

That's why C++-style casting is there IMO: it stands out like a sore
thumb, making you think about it more .

I think you worry too much there. Unless you go completely crazy with
casting, risk should be quite manageable.

Goran.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-14-2011
On 4/14/2011 6:41 AM, crea wrote:
> This would definitely work:
> template<class T> class Data
> {
> public:
> T p;
> };
>
> template<class T> class AA
> {
> public:
> Data<T> m_data;
> };
>
> template<class T> class BB : public AA


'AA' is missing the template arguments. Won't compile. Did you mean to say

template<class T> class BB : public AA<T>

?

> {
> public:
> };
>
> So I could create object like this:
>
> AA<int> b;
> BB<string> b;


You can't create two objects of different types with the same name in
the same scope.

>
> Ok, this definitely works,


Nope.

> but its a bit difficult to put all of those
> templates in all classes. I was thinking that because the only thing I need
> here is that BB has different Data: type than AA, so it could be done
> hidden in BB class somehow. So there is no need to tell it when calling:
>
> BB<string> b;
>
> but rather just:
> BB b;
>
> I dont think we need to tell the type when creating objects, because the
> classes already know what type it should be (if created an object from BB it
> ALWAYS has string type and AA ALWAYS has int type - thats the situation).
>
>


V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
crea
Guest
Posts: n/a
 
      04-14-2011

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:io6t1k$v7f$(E-Mail Removed)...
> On 4/14/2011 6:41 AM, crea wrote:
>> This would definitely work:
>> template<class T> class Data
>> {
>> public:
>> T p;
>> };
>>
>> template<class T> class AA
>> {
>> public:
>> Data<T> m_data;
>> };
>>
>> template<class T> class BB : public AA

>
> 'AA' is missing the template arguments. Won't compile. Did you mean to
> say
>
> template<class T> class BB : public AA<T>
>
> ?


yes, sorry... I did not compile the code, so might have errors.

>
>> {
>> public:
>> };
>>
>> So I could create object like this:
>>
>> AA<int> b;
>> BB<string> b;

>
> You can't create two objects of different types with the same name in the
> same scope.


ye, should be c the other one...

>
>>
>> Ok, this definitely works,

>
> Nope.


after corrections yes..



 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      04-14-2011
On 4/14/2011 2:59 AM, crea wrote:
>> 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).


I don't really understand the question so I'll try to answer the various
questions you could be asking. To override m_data it's as easy as

class BB : public AA
{
public:
string m_data; // was declared int in AA.
};

Now though you have a class with two m_data objects in it depending on
scope.

The other thing you seem to be asking is how to declare a member in a
template based on the instantiation parameter type. This can be done
two different ways. Luckily, the two ways can be combined nicely:

template < typename T >
struct member_type { typedef typename T::mem_t type; };

template < >
struct member_type<AA> { typedef int type; };
template < >
struct member_type<BB> { typedef string type; };

template < typename T >
class Data
{
public:
typedef typename member_type<T>::type m_data_t;
m_data_t m_data;
};

Of course, there's a third option which uses mpl::if_ but it would scale
as well as a turd.

--
http://crazycpp.wordpress.com
 
Reply With Quote
 
crea
Guest
Posts: n/a
 
      04-15-2011

"Noah Roberts" <(E-Mail Removed)> wrote in message
news:4da720e7$0$3286$(E-Mail Removed)...
>
> I don't really understand the question so I'll try to answer the various
> questions you could be asking. To override m_data it's as easy as
>
> class BB : public AA
> {
> public:
> string m_data; // was declared int in AA.
> };
>
> Now though you have a class with two m_data objects in it depending on
> scope.


I think you understand my point... I tried this and it works (I actually
found it myself before, this idea). But is this good programming for sure?
This is all according to c++ rules to do like this? Because now when we are
creating a BB-object then int m_data; - member is also created even though
we never use it for that object. Is this good programming... I am just
thinking. So there is created one member variable that is never used (int
m_data; in AA).

Other than that, this actually would do the job for me.

My point is that: for objects created from BB I want to have a m_data member
created with type string:

Data<string> m_data;

and for objects created from AA I want a m_data member created with type
int:

Data<int> m_data;

>
> The other thing you seem to be asking is how to declare a member in a
> template based on the instantiation parameter type. This can be done two
> different ways. Luckily, the two ways can be combined nicely:
>
> template < typename T >
> struct member_type { typedef typename T::mem_t type; };
>
> template < >
> struct member_type<AA> { typedef int type; };
> template < >
> struct member_type<BB> { typedef string type; };
>
> template < typename T >
> class Data
> {
> public:
> typedef typename member_type<T>::type m_data_t;
> m_data_t m_data;
> };
>


I think you might be into something here, but this is quite complex for me
so takes some time to understand this code . I ll let you know. This might
be even better than your first suggestion because we would not create a
variable which is never used.


> Of course, there's a third option which uses mpl::if_ but it would scale
> as well as a turd.



 
Reply With Quote
 
crea
Guest
Posts: n/a
 
      04-17-2011

> The other thing you seem to be asking is how to declare a member in a
> template based on the instantiation parameter type. This can be done two
> different ways. Luckily, the two ways can be combined nicely:
>
> template < typename T >
> struct member_type { typedef typename T::mem_t type; };
>
> template < >
> struct member_type<AA> { typedef int type; };
> template < >
> struct member_type<BB> { typedef string type; };
>
> template < typename T >
> class Data
> {
> public:
> typedef typename member_type<T>::type m_data_t;
> m_data_t m_data;
> };
>


Can you then also show my how to use this (in main function)? I am a bit
lost , because seems like its not exatcly the same as my example.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Plz, a simple answer to a simple question about IP addresses MeekiMoo Computer Support 0 07-28-2009 08:10 AM
Simple region code question... simple answer?? joseph.greer@gmail.com DVD Video 7 01-26-2007 09:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM



Advertisments