Sign in to follow this  

functions and arrays within a class - scope question

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, in C++, data members of a class have global scope within that class. by that reasoning, it shouldn't be absolutely nessacary to pass data members to functions within the same class. Is this correct? I'm useing visual c++ 6, just in case this varies from compiler to compiler. thanks for your time.

Share this post


Link to post
Share on other sites
you never have to pass member variables to member functions within the same class instance:


class Example
{
public:
int getFirstPlusSecond();

private:
int first;
int second;
};

int Example::getFirstPlusSecond()
{
return first + second;
}





-me

Share this post


Link to post
Share on other sites
If you mean - can you do this:


class MyClass
{
int x_;
public:
MyClass(int x) : x_(x) {}
void SetX(int x) {x_ = x;}
int GetX() const {return x_;}
};



Then yes, no worries.

Jim.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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