PDA

View Full Version : How to call super class method from sub class



mkkguru
2nd February 2010, 12:39
Hi to all,

Please tell me how to call super class methods from subclass ,when iam calling superclass method from subclass me getting error as shown below.



In file included from eclogic_lnx.h:8,
/eclogic_lnx.h:8: In file included from eclogic_lnx.h:8,
/eclogic.h:98: error: ISO C++ forbids declaration of ‘eclogic_lnx’ with no type
/eclogic.h:98: error: expected ‘;’ before ‘*’ token


and method iam calling like this


// eclogic_lnx is the subclass
void eclogic_lnx::on_buttonBox_clicked(QAbstractButton* button)
{
if(button)

eclogic->createTreewidget(); //eclogic is the super class and createTreewidget is the super class method name
}

plz tell me how to call and write the code ,
thanks in advance

high_flyer
2nd February 2010, 12:58
Read some C++ tutorials.
You can't program if you didn't learn/read anything about it.
It is not in the scope of this forum to teach elementary C++ (OOP) concepts and syntax.

aamer4yu
2nd February 2010, 13:04
are you sure the error is because of calling super class function issue ?
and I would suggest please read some basic C++ book first. You can easily find answer to your question.

mkkguru
3rd February 2010, 05:16
are you sure the error is because of calling super class function issue ?
and I would suggest please read some basic C++ book first. You can easily find answer to your question.



ok thakq, As iam new to this Qt,here some conventions are different ,so getting problem, as i didnt understand relating to that error?

aamer4yu
3rd February 2010, 07:46
here some conventions are different
Can you point some ? Qt is a C++ framework. All C++ conventions follow.

high_flyer
3rd February 2010, 08:38
This has nothing to do with "different" conventions, but with basic C++.
In your case, calling a mother class function (note: 'mother' here means the class from which you derived, not the a parent object) can be done by ClassName::methodName(). (as long as the method is not private)
But you didn't even specify if the mother class should be an instance, or the current class just wants to access functionality in the mother class.

Again, you wont be able to avoid reading the basics first!

pitonyak
3rd February 2010, 14:58
Given the initial comments posted to the thread, I expected this thread to die, but it has not. I thought that moderator might move the thread to the General thread, but it is still here, so....

You can directly call a method for a specific class in C++ providing you have visibility to see the class and the method in the class. The example that I provided to the original poster using a direct email is as follows:


class LTop
{
public:
virtual void notInMid() const
{
qDebug("Method LTop::notInMode()");
}
virtual void allDo() const
{
qDebug("Method LTop::allDo()");
}
};
class LMid : public LTop
{
typedef LTop Base;
public:
virtual void allDo() const
{
Base::allDo();
qDebug("Method LMid::allDo()");
}
};
class LBottom : public LMid
{
typedef LMid Base;
public:
virtual void notInMid() const
{
//Cannot make this call because it does not exist
//Base::notInMid();
//Only works if there is public inheritance.
LTop::notInMid();
qDebug("Method LBottom::notInMode()");
}
virtual void allDo() const
{
//this->LMid::allDo();
Base::allDo();
qDebug("Method LBottom::allDo()");
}
};


Notice a few things that allow this to work


Public inheritance is used. Using "class LMid : LTop" rather than "class LMid : public LTop" prevents LBottom from referencing a method in LTop, but still allows LMid to reference LTop.
The methods must have visibility. In this example, I used public:
You cannot call a method directly that does not exist. Consider the method "notInMid". The method is not implemented in LMid. I dislike that in LBottom I cannot simply say "call the next one up in the heirarchy". I am sure that this is because C++ supports multiple inheritance, so it is not obvious what to call unless you explicitly specify it.

The probably enough for now.

aamer4yu
4th February 2010, 04:54
@Pitonyak
Please dont encourage such questions. I agree with high_flyer post #2 -

It is not in the scope of this forum to teach elementary C++ (OOP) concepts and syntax.

pitonyak
4th February 2010, 05:21
Celebrate Ferrum

pitonyak
4th February 2010, 05:29
@Pitonyak
Please dont encourage such questions. I agree with high_flyer post #2 -

The slightest thought had not even begun to speculate on the merest possibility of crossing my mind... Until after continued dialog on this thread.