rooni
rooni

Reputation: 1090

What should i do to access member variable of an object?

#include<iostream>
using namespace std;
int main(){
    class c1{
        int i;

        public:
        int bv;
        void seti(int i){
            c1::i=i;
                        }
        int geti(){return i;}
        int accessbv(c1 inst1);
    };
    int c1::accessbv (c1 inst1){
        cout<<"here in the base accesing inst1's bv"<<inst1.bv;
    }

I want to access inst1's member variable bv.The above code doesnt compile. What should i do? Also i have observed one thing ,if the class c1's defintion is global it is working the way i want it to. Why is it behaving this way?

Upvotes: 1

Views: 103

Answers (2)

Francis Cugler
Francis Cugler

Reputation: 7925

To expand on StoryTeller's answser here is an example of explaining what it means by your comment to his answer:

can you clarify the first statement "An enclosing function has no special access to members of the local class; it obeys the usual access rules (Clause [class.access])." and add something about no functions allowed inside another function rule.

void someFunc() {
    class foo {
    private:
        int x;
    public:
        int getX() const { return x; }
        void setX( int val ) { x = val; }
    };
}

Here foo is a local class that is declared in someFunc(). The scope or body of someFunc has no means of accessing foo's internals. Since the above functions are declared & defined within foo's scope this is valid.

Trying to do this:

void someFunc() {
    class boo {
    private:
        int x;
    public:
        int getX() const;
        void setX( int val );
    };

    // These are illegal. You can not define a function within a function
    // even when they belong to a class.

    // These two functions can not be defined here because this is
    // considered re-declaring them out side of their class within local scope of a function. 
    // These will give you a compiler error: local function definitions are illegal
    int boo::getX() const { return x; }
    void boo::setX( int val ) { x = val; }
} 

Results in a compiler error.

Here someFunc() has no way of accessing boo's scope through its scope resolution operator for its member variables. It can not access boo::x.

It is illegal to declare a function within a function this can not be done because of the stack pointer for function scope. Functions are not like while & for loops or switch statements which can be nested. A function's scope is a single translation unit within an object file after it's been compiled. You can have an empty scope within a function as that is valid, but you can not declare - define a function within another.

Upvotes: 1

Just because the class is local, doesn't mean the rules for defining functions change. The C++ standard even goes as far as to state it explicitly.

[class.local]/2, emphasis mine:

An enclosing function has no special access to members of the local class; it obeys the usual access rules (Clause [class.access]). Member functions of a local class shall be defined within their class definition, if they are defined at all.

"The rules" mentioned above is simply what is stated in the function definition related section of the standard, [dcl.fct.def.general]/2:

... A function shall be defined only in namespace or class scope.

And that's it.

Upvotes: 2

Related Questions