alias for variables obtained by inheritance

A

aaragon

Hi everyone,

I wrote some code for a class but then I decided that I wanted to
inherit from this class. Well, it turns out that now I have to put
"this->" in front of all the member variables of the class that I'm
inheriting from in order to compile correctly. Is there another way to
do this? There has to be a way to define an alias command so I can
define it once and then use my original names.

Thank you,

a^2
 
T

Thomas Tutone

aaragon said:
I wrote some code for a class but then I decided that I wanted to
inherit from this class. Well, it turns out that now I have to put
"this->" in front of all the member variables of the class that I'm
inheriting from in order to compile correctly. Is there another way to
do this? There has to be a way to define an alias command so I can
define it once and then use my original names.


Aaragon, you've posted many times before, so you know the drill. If
you want good responses to your question, you need to post the smallest
compilable piece of code that exhibits the problem you're describing.
Otherwise, we're just using our crystal balls to diagnose your problem,
and our crystal balls just aren't as accurate as we'd like.

Offhand, it sounds like you're inheriting from a template class, but
I'd need to see the code to be sure.

Best regards,

Tom
 
A

aaragon

Thomas said:
Aaragon, you've posted many times before, so you know the drill. If
you want good responses to your question, you need to post the smallest
compilable piece of code that exhibits the problem you're describing.
Otherwise, we're just using our crystal balls to diagnose your problem,
and our crystal balls just aren't as accurate as we'd like.

Offhand, it sounds like you're inheriting from a template class, but
I'd need to see the code to be sure.

Best regards,

Tom

Thanks for replying Tom. The thing is that the code is huge. Now I have
to change the design and I run into this problem of renaming all the
member variables because for some reason the compiler doesn't recognize
that those variables were inherited. I'm gonna try to summarize the
code as follows:

template
<
typename class1class classA
{
protected:

// statistics variables
class1 varClass1;
size_t var1;
size_t var2;
size_t var3;
};

template
<
typename class1class classB : public classA<class1>
{
void statistics()
{
cout<<"class1 -> "<<varClass1.print()<<endl;
cout<<"var1 = "<<var1<<endl;
cout<<"var2 = "<<var2<<endl;
cout<<"var3 = "<<var3<<endl;
}
};

I wrote this code because it shows the problem that I have right now.
The problem is that if I don't put the "this->" before the member
variables that were inherited (var1, var2, var3 and varClass1), the
compiler gives me a message as if those variables don't exist. Then, I
thought that it may be a way to set an alias for them. Any ideas?
 
T

Thomas Tutone

aaragon said:
Thanks for replying Tom. The thing is that the code is huge. Now I have
to change the design and I run into this problem of renaming all the
member variables because for some reason the compiler doesn't recognize
that those variables were inherited. I'm gonna try to summarize the
code as follows:

template
<
typename class1
class classA
{
protected:

// statistics variables
class1 varClass1;
size_t var1;
size_t var2;
size_t var3;
};

template
<
typename class1
class classB : public classA<class1>
{
void statistics()
{
cout<<"class1 -> "<<varClass1.print()<<endl;
cout<<"var1 = "<<var1<<endl;
cout<<"var2 = "<<var2<<endl;
cout<<"var3 = "<<var3<<endl;
}
};

I wrote this code because it shows the problem that I have right now.
The problem is that if I don't put the "this->" before the member
variables that were inherited (var1, var2, var3 and varClass1), the
compiler gives me a message as if those variables don't exist. Then, I
thought that it may be a way to set an alias for them. Any ideas?

Nice to know my crystal ball was working just fine.

This is an FAQ:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.19

Best regards,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top