473,378 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

calling class method

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 ?

Jan 3 '08 #1
9 1570
mosfet wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 3 '08 #2
On 2008-01-03 11:02:00 -0500, mosfet <jo******@anonymous.orgsaid:
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();

--

-kira

Jan 3 '08 #3
Victor Bazarov a écrit :
mosfet wrote:
>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
Does it mean that a running B object holds also A methods it has
redefined or is it done at compilation time ?
Jan 3 '08 #4
mosfet wrote:
Victor Bazarov a écrit :
>mosfet wrote:
>>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
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 3 '08 #5
In article <2008010311314116807-kirakun@earthlinknet>,
Kira Yamato <ki*****@earthlink.netwrote:
On 2008-01-03 11:02:00 -0500, mosfet <jo******@anonymous.orgsaid:
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();
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;
}
Jan 3 '08 #6
shaun roe wrote:
In article <2008010311314116807-kirakun@earthlinknet>,
Kira Yamato <ki*****@earthlink.netwrote:
>On 2008-01-03 11:02:00 -0500, mosfet <jo******@anonymous.orgsaid:
>>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();

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 3 '08 #7
>
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...
Jan 3 '08 #8
Rahul wrote:
>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...
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 3 '08 #9
On Jan 3, 10:22 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Rahul wrote:
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...

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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...
Jan 3 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
7
by: Klaus Friese | last post by:
Hi, i'm currently working on a plugin for Adobe InDesign and i have some problems with that. I'm not really a c++ guru, maybe somebody here has an idea how to solve this. The plugin is...
5
by: Dave Veeneman | last post by:
I'm using inheritance more than I used to, and I find myself calling a lot of base class methods. I generally call a base method from a dreived class like this: this.MyMethod(); I'm finding...
7
by: JJ | last post by:
Hi, I call a class in my windows service app and in that class I access a method that returns an OleDbReader. Now It does have records in the reader when I step through the method but when I...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
12
by: Ron | last post by:
Greetings, I am trying to understand the rational for Raising Events instead of just calling a sub. Could someone explain the difference between the following 2 scenarios? Why would I want to...
0
by: han zhiyang | last post by:
I've just studied the "how to" web service and the async pattern in donnet.I make a test with these knowledges,but I got a strange result. Here is my test. 1.Write a simple "Add" service named...
6
by: Anthony Smith | last post by:
I can call a class using "->", but it complains about the :: I see on the net where :: is used. Is there a good explanation on when to use one over the other or the differences? $help = new...
10
by: Finger.Octopus | last post by:
Hello, I have been trying to call the super constructor from my derived class but its not working as expected. See the code: class HTMLMain: def __init__(self): self.text = "<HTML><BODY>";...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.