I know some languages allow this. Is it possible in C++?
Asked
Active
Viewed 143 times
2
-
Do you mean a) from within the derived class or b) from a function not in the related classes? – Andres Jaan Tack Oct 25 '09 at 02:35
-
possible duplicate of [C++: How to call a parent class function from derived class function?](http://stackoverflow.com/questions/357307/c-how-to-call-a-parent-class-function-from-derived-class-function) – outis Apr 22 '12 at 00:05
3 Answers
5
Yes:
#include <iostream>
class X
{
public:
void T()
{
std::cout << "1\n";
}
};
class Y: public X
{
public:
void T()
{
std::cout << "2\n";
X::T(); // Call base class.
}
};
int main()
{
Y y;
y.T();
}

Martin York
- 257,169
- 86
- 333
- 562
2
class A
{
virtual void foo() {}
};
class B : public A
{
virtual void foo()
{
A::foo();
}
};

Paul Lalonde
- 5,020
- 2
- 32
- 35
1
Yes, just specify the type of the base class.
For example:
#include <iostream>
struct Base
{
void func() { std::cout << "Base::func\n"; }
};
struct Derived : public Base
{
void func() { std::cout << "Derived::func\n"; Base::func(); }
};
int main()
{
Derived d;
d.func();
}

janm
- 17,976
- 1
- 43
- 61