# Class method with the same name?

This topic is 2299 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi folks,

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

 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); } 

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 on other sites
Class declaration needs ending semicolon!
And it's pMyClass, not pMyclass

class CMyClass
{
public:
void MyMethod( int a, int b);
}

# Class->MyMethod( 1, 2, 3); }

##### Share on other sites
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 using CMyClass::MyMethod; in the CMyNextClass definition.

##### Share on other sites
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 on other sites

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.

Otherwise, you code is fine and should not cause 3 parameters error.

##### Share on other sites
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 using CMyClass::MyMethod; in the CMyNextClass definition.

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 on other sites

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?

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 on other sites

[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?

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.

 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! }; 

##### Share on other sites

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.
]

Not exactly.

##### Share on other sites

[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.
]

Not exactly.
[/quote]

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

1. 1
Rutin
26
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 21
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631763
• Total Posts
3002190
×