1

I am not sure is my question is right or not? But let me still try to ask once.

I have a Class with have few member variables defined. As per OO concepts, every member function can access , all member variables of its class.

But I want these member variable to be accessed via specific methods (Lets say Getters) , even within same class member functions.

It there any way to do it?

class A {

public:

void func1();
void func2();
B getB();

private:
B b;
}

void A::func1() {

b.functionFromB(); // function uses member variable b directly
}

void A::func2() {
B b1=getB();  // function ask for B from a function and then uses it. // I need something like this... And ensure each function uses same way otherwise there should be warning...
b1.functionFromB();
}

Thanks, Kailas

Kailas
  • 807
  • 1
  • 6
  • 20

3 Answers3

4

No, there is not. You can do it via encapsulation and inheritance like:

class shape
{
    private:
        int angles;
    protected:
        shape(int angles_):angles(angles_){}; 
        int getAngles() const;
}

class square : private shape
{
    public:
        square():shape(4){}
        void doSth()
        {
            \\ you can access angles only via getAngles();
        }    
}
clsbartek
  • 186
  • 1
  • 3
  • 18
1

Any private members of the class can be accessed from within the class, but not by users of the class. So it looks like you need private members and public methods that allow access to them.

class A
{
  private:
      int a;
  public:
      int getA() {return a;}

};

int main()
{
  A inst;
  int t;

  inst.a =5; // error member a is private
  t = inst.getA(); //OK
}

The concept extends fine to nested class declarations in case you only want to allow instance of a class to be created from another class; details here

Community
  • 1
  • 1
Pandrei
  • 4,843
  • 3
  • 27
  • 44
  • I think thats a good example to demonstrate the advantage of OOP.+1 – aGer Nov 05 '15 at 09:40
  • This is not what she or he is looking for. – Emil Laine Nov 05 '15 at 10:27
  • Yes.. I am not looking for this.. I want to restrict direct access to variable / atleast warning on direct access of variable from methods of same class. I think there is no other option than using inheritance here.. – Kailas Nov 05 '15 at 11:04
  • @Kailas I'm not sure what you mean; maybe give an example in the question show exactly what you want to prevent? – Pandrei Nov 05 '15 at 14:37
  • @Kailas it sounds like you want to use nested classes; I included a link with pretty good explanation. – Pandrei Nov 05 '15 at 16:06
0

As others have said - you have to add an additional layer.

If you want to give access to specific methods then you can use the friend keyword. E.g.

// Public.h
#pragma once

class Public
{
public:
    Public();

    int GetI() const;
    float GetF() const;

private:
    std::unique_ptr<Private> p_;
};

//Public.cpp
#include "Public.h"

Public::Public()
    : p_(new Private)
{
}

int Public::GetI() const
{
    return p_->i_;
}

float Public::GetF() const
{
    return p_->f_;
}

// Private.h
#pragma once

class Private
{
    friend int Public::GetI() const;
    friend float Public::GetF() const;

    int i_;
    float f_;
};

Keep in mind that every friend method can access ALL private members.

If you really really want to limit which methods can access which members then you can wrap each member in a separate class/struct and make only the getter/setter of that member a friend of that class/struct but I would not recommend this approach.

Mohamad Elghawi
  • 2,071
  • 10
  • 14