Sign in to follow this  
amateur programmer

using the same variable name twice in C++

Recommended Posts

in java code u can have two variables with the same name eg class b { String name =""; public void method(String name) { = name; } } is there a way u can do this in c++. That exact code in C++ generates an error request for member y in this

Share this post

Link to post
Share on other sites
You don't actually have two variables of the same name in that snippet. You have a variable B::name and a local variable (arg to function) called name. The exact same construct is possible in C++. The reason you got an error is because you tried to compile Java code with a C++ compiler.

You could do this:

class B
char *name;

void SetName(char *name) { this->name = name; }

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