Sign in to follow this  
GodFear

Class method with the same name?

Recommended Posts

GodFear    151
Hi folks,

Howdy?!
I need help on a C++ class methods.
Say my class is:

[CODE]
class CMyClass
{
public:
void MyMethod( int a, int b);
}
class CMyNextClass: public CMyClass
{
public:
void MyMethod( int a, int b, int c);
}
void main()
{
CMyNextClass* pMyClass = new CMyNextClass();

pMyclass->MyMethod( 1, 2, 3);
}

[/CODE]

I get an compiler error saying MyMethod function does not take 3 arguments.
I am very new to C++ and classes and is still in learning process.
I am trying to google if what i am doing violates C++ rule but i can't find an answer. I appreciate if someone can help.
Thanks!

Share this post


Link to post
Share on other sites
wqking    761
Class declaration needs ending semicolon!
And it's pMyClass, not pMyclass

class CMyClass
{
public:
void MyMethod( int a, int b);
}[size=6][b];[/b][/size]
class CMyNextClass: public CMyClass
{
public:
void MyMethod( int a, int b, int c);
}[size=6][b];[/b][/size]
void main()
{
CMyNextClass* pMyClass = new CMyNextClass();

pMy[size=6][b]C[/b][/size]lass->MyMethod( 1, 2, 3);
}

Share this post


Link to post
Share on other sites
SiCrane    11839
That should actually give you a "class does not take two arguments" error when called with two arguments. Three arguments should be fine. If you want it to take two arguments then you would insert a [tt]using CMyClass::MyMethod;[/tt] in the CMyNextClass definition.

Share this post


Link to post
Share on other sites
GodFear    151
ok my bad, i really didn't copy and paste my code. instead i just manually typed it.
The real code has semi colons at the ending as you have shown.

Also pMyClass->MyMethod( 1, 2, 3 ) is what i actually have in my code.

The compiler error i am getting is that it says that "MyMethod" does not take 3 arguments. It seems like it is expecting the MyMethod function declared and defined in the base class CMyClass.
I am not sure if i am violating the rules of function overloading in my code, but i cannot figure it out.

Share this post


Link to post
Share on other sites
wqking    761
[quote name='GodFear' timestamp='1333374552' post='4927486']
ok my bad, i really didn't copy and paste my code. instead i just manually typed it.
The real code has semi colons at the ending as you have shown.

Also pMyClass->MyMethod( 1, 2, 3 ) is what i actually have in my code.

The compiler error i am getting is that it says that "MyMethod" does not take 3 arguments. It seems like it is expecting the MyMethod function declared and defined in the base class CMyClass.
I am not sure if i am violating the rules of function overloading in my code, but i cannot figure it out.
[/quote]
Then show your real code.
Otherwise, you code is fine and should not cause 3 parameters error.

Share this post


Link to post
Share on other sites
GodFear    151
[quote name='SiCrane' timestamp='1333374265' post='4927484'] That should actually give you a "class does not take two arguments" error when called with two arguments. Three arguments should be fine. If you want it to take two arguments then you would insert a [tt]using CMyClass::MyMethod;[/tt] in the CMyNextClass definition. [/quote]

You are correct! I think my brain just dried out figuring out this problem. so if the class that inherits a base class and declare a method with the same name(different parameters) as one in the base class, it is considered an violation then?

Share this post


Link to post
Share on other sites
wqking    761
[quote name='GodFear' timestamp='1333374764' post='4927489']
You are correct! I think my brain just dried out figuring out this problem. so if the class that inherits a base class and declare a method with the same name(different parameters) as one in the base class, it is considered an violation then?
[/quote]
No, that's not violation.
But the function in your derived class will hide the visibility of the function in the base class.
That's why SiCrane suggested use "using BaseClass:SomeFunction;" in your derived class.

Share this post


Link to post
Share on other sites
GodFear    151
[quote name='wqking' timestamp='1333374940' post='4927491']
[quote name='GodFear' timestamp='1333374764' post='4927489']
You are correct! I think my brain just dried out figuring out this problem. so if the class that inherits a base class and declare a method with the same name(different parameters) as one in the base class, it is considered an violation then?
[/quote]
No, that's not violation.
But the function in your derived class will hide the visibility of the function in the base class.
That's why SiCrane suggested use "using BaseClass:SomeFunction;" in your derived class.
[/quote]

Thank you.
So to clarify, i can overload a method within a class. but i cannot overload a method from a base class in the derived class.
Am i correct to say this? If yes, is this a rule? I am trying to figure out why would the derived class hide the visibility of MyMethod() function in the base class.

[CODE]
class CBaseClass
{
public:
void MyMethod( int a );
};
class CDerivedClass: public CBaseClass
{
public:
void MyMethod( int a, int b ); // error!
void AnotherMethod( int a );
void AnotherMethod( int a, int b ); // ok!
};
[/CODE]

Share this post


Link to post
Share on other sites
wqking    761
[quote name='GodFear' timestamp='1333375822' post='4927500']
Thank you.
So to clarify, i can overload a method within a class. but i cannot overload a method from a base class in the derived class.
Am i correct to say this? If yes, is this a rule? I am trying to figure out why would the derived class hide the visibility of MyMethod() function in the base class.
]
[/quote]
Not exactly.
This link may help you
http://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the

Share this post


Link to post
Share on other sites
GodFear    151
[quote name='wqking' timestamp='1333433194' post='4927790']
[quote name='GodFear' timestamp='1333375822' post='4927500']
Thank you.
So to clarify, i can overload a method within a class. but i cannot overload a method from a base class in the derived class.
Am i correct to say this? If yes, is this a rule? I am trying to figure out why would the derived class hide the visibility of MyMethod() function in the base class.
]
[/quote]
Not exactly.
This link may help you
[url="http://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the"]http://stackoverflow...verloads-of-the[/url]
[/quote]

Wow! That link just clarified everything!
Thanks for the link, i really appreciate the help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this