calling class method

M

mosfet

Hi,

Let's say I have two classes A and B with B inheriting from A
with one non virtual method :


class A
{
public:

void MethodA()
{
cout << "MethodeA in class A" << endl
}
};


class B : public class A
{
public:

void MethodA()
{
cout << "MethodeA in class B" << endl
}
};




int main()
{

B testB;

testB.MethodA(); // Should display "MethodeA in class B"

}


IF I try this example the MethodA from class B will be called.
And what if I want from a B object to call the A implementation ? Is it
possible ?
 
V

Victor Bazarov

mosfet said:
Let's say I have two classes A and B with B inheriting from A
with one non virtual method :


class A
{
public:

void MethodA()
{
cout << "MethodeA in class A" << endl
}
};


class B : public class A
{
public:

void MethodA()
{
cout << "MethodeA in class B" << endl
}
};




int main()
{

B testB;

testB.MethodA(); // Should display "MethodeA in class B"

}


IF I try this example the MethodA from class B will be called.
And what if I want from a B object to call the A implementation ? Is
it possible ?

Yes, do

testB.A::MethodA();

HTH

V
 
K

Kira Yamato

Hi,

Let's say I have two classes A and B with B inheriting from A
with one non virtual method :


class A
{
public:

void MethodA()
{
cout << "MethodeA in class A" << endl
}
};


class B : public class A
{
public:

void MethodA()
{
cout << "MethodeA in class B" << endl
}
};




int main()
{

B testB;

testB.MethodA(); // Should display "MethodeA in class B"

}


IF I try this example the MethodA from class B will be called.
And what if I want from a B object to call the A implementation ? Is it
possible ?

testB.A::MethodA();
 
M

mosfet

Victor Bazarov a écrit :
Yes, do

testB.A::MethodA();

HTH

V
Does it mean that a running B object holds also A methods it has
redefined or is it done at compilation time ?
 
V

Victor Bazarov

mosfet said:
Victor Bazarov a écrit :
Does it mean that a running B object holds also A methods it has
redefined or is it done at compilation time ?

A "running B object"? "Holds"? Not sure what you mean by all these
terms, sorry. The functions do not go anywhere. The calls are
resolved at compile time (since your functions are non-virtual).
The name resolution is a well defined process. Since 'B' inherits
from 'A', any members of 'A' are also members of 'B', and you can
call them, nothing to it. You only need to let your compiler know
where to find the member function you need to call.

V
 
S

shaun roe

Kira Yamato said:
testB.A::MethodA();

like so?:

#include <iostream>
using namespace std;

class A{
public:
void methodA(){
cout << "Method A in class A" << endl;
}
};


class B : public A{
public:
void methodA(){
cout << "Method A in class B" << endl;
}
void methodAfromA(){
A::methodA();
}
};


int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
B testB;

testB.methodA(); // Should display "Method A in class B"
testB.methodAfromA();
return 0;
}
 
V

Victor Bazarov

shaun said:
Kira Yamato said:
testB.A::MethodA();

like so?:

#include <iostream>
using namespace std;

class A{
public:
void methodA(){
cout << "Method A in class A" << endl;
}
};


class B : public A{
public:
void methodA(){
cout << "Method A in class B" << endl;
}
void methodAfromA(){
A::methodA();
}
};


int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
B testB;

testB.methodA(); // Should display "Method A in class B"
testB.methodAfromA();
return 0;
}

Why bother with all this? Just do as Kira has suggested, in the
'main' function:

int main() {
B testB;
testB.A::MethodA();
}

Or do you have a problem with that?

V
 
R

Rahul

A "running B object"? "Holds"? Not sure what you mean by all these
terms, sorry. The functions do not go anywhere. The calls are
resolved at compile time (since your functions are non-virtual).
The name resolution is a well defined process. Since 'B' inherits

Well, the calls to even a virtual function would be resolved at
compile time, if they are called using the object rather than the
pointers...
 
V

Victor Bazarov

Rahul said:
Well, the calls to even a virtual function would be resolved at
compile time, if they are called using the object rather than the
pointers...

Yes, in this case they are. Just to emphasize, calls to non-virtual
functions are _always_ resolved at compile time regardless of how the
call is made, from a pointer, a reference, or an object. It probably
is the question of how it's implemented: do you check the virtuality
first or whether it's called through a pointer/reference or an object
first...

V
 
R

Rahul

Yes, in this case they are. Just to emphasize, calls to non-virtual
functions are _always_ resolved at compile time regardless of how the
call is made, from a pointer, a reference, or an object. It probably
is the question of how it's implemented: do you check the virtuality
first or whether it's called through a pointer/reference or an object
first...

V

Yes, and the irony being that even a call to virtual functions with a
pointer or a reference is taken care in compile time with the help of
function pointers, a kind of relative call (from the begining of
VTABLE)... if i'm correct, this is often done by the static linker...
 

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

Forum statistics

Threads
474,184
Messages
2,570,976
Members
47,536
Latest member
MistyLough

Latest Threads

Top