How do I call the parent function from
a derived class using C++? For example, I have a class called
parent, and a class called child which
is derived from parent. Within
each class there is a
print function. In the definition of the child's print function
I would like to make a call to the parents print function. How would I go about doing
this?
Friday, 8 December 2017
c++ - How to call a parent class function from derived class function?
Answer
I'll take the risk of stating the obvious:
You call the function, if it's defined in the base class it's automatically available in
the derived class (unless it's
private).
If
there is a function with the same signature in the derived class you can disambiguate it
by adding the base class's name followed by two colons
base_class::foo(...). You should note that unlike Java and C#,
C++ does not have a keyword for "the base class"
(super or base) since C++ supports
rel="noreferrer">multiple inheritance which may lead to
ambiguity.
class left
{
public:
void foo();
};
class right
{
public:
void
foo();
};
class bottom : public left, public
right {
public:
void foo()
{
//base::foo();//
ambiguous
left::foo();
right::foo();
// and when foo() is not called for
'this':
bottom b;
b.left::foo(); // calls b.foo() from
'left'
b.right::foo(); // call b.foo() from 'right'
}
};
Incidentally,
you can't derive directly from the same class twice since there will be no way to refer
to one of the base classes over the
other.
class bottom :
public left, public left { //
Illegal
};
php - file_get_contents shows unexpected output while reading a file
I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...
-
I would like to split a String by comma ',' and remove whitespace from the beginning and end of each split. For example, if I have ...
-
I have an app which needs a login and a registration with SQLite. I have the database and a user can login and register. But i would like th...
-
I would like to get the JSON data from Coinmarket. I get the data but can't map the Data. below my Code const p = document.q...
No comments:
Post a Comment