简体   繁体   English

C ++保护访问

[英]C++ protected access

is there anyway i can access a protected variable in a class without inheritance. 无论如何,我可以在没有继承的情况下访问类中的受保护变量。

class ClassA{
  protected:
    int varA; 
};

class ClassB{
  protected:
    ClassA objectA;

};


ClassB theMainObject;

I would like to access varA through theMainObject. 我想通过theMainObject访问varA。

You could make classB friend of classA 你可以做classB的朋友classA

class ClassA{
  protected:
    int varA; 

  friend ClassB;
}

but using a accessors would probably be better since you are not coupling the classes together. 但是使用访问器可能会更好,因为你没有将类耦合在一起。

class ClassA{
  int getA() { return varA;}
  void setA(int a) { varA = a; }
  protected:
    int varA; 
}

Have an accessor function is the only way ie; 有一个访问者功能是唯一的方式,即;

public:
   int getVarA(){return varA;}

Add standard getter/setter functions: 添加标准的getter / setter函数:

int ClassA::GetVarA()  
{  
    return varA;
}  
BOOL ClassA::SetVarA(int nNewVar)  
{  
    // Perform verifications on nNewVar... Return FALSE if didn't go well.  

    // We're satisfied. Set varA to the new value.  
    varA = nNewVar;  

    return TRUE;
}

friend是你的friend通过提及你想要在你想要使用的班级中作为朋友访问的班级来使用该关键词!

You can't, and quite possibly shouldn't, access varA directly from theMainObject . 你不能,很可能不应该访问varA直接从theMainObject protected is the same as private for unrelated classes (such as your two example class) so varA has no visibility. protected是一样的私立无关的类(比如你的两个例子类),所以varA有没有知名度。 Use ClassA 's normal API to manipulate its state. 使用ClassA的普通API来操纵它的状态。

If varA is part of the result state of ClassA and you just need read access to it, then you should add a public accessor to that class: 如果varAClassA结果状态的一部分,并且您只需要对它的读访问权限,那么您应该为该类添加一个公共访问器:

public:
   int getVarA() const
   {
       return varA;
   }

Besides of making ClassB a friend of ClassA there are some standard and non-standard hacks to get to ClassA internals. 除了使ClassB成为ClassA的朋友之外,还有一些标准和非标准的黑客可以进入ClassA内部。 You can read Herb Sutter's article about them. 你可以阅读Herb Sutter关于它们的文章

As Mark B mentionned, solution proposed by Hovhannes Grigoryan is not safe and may have unexpected behaviour because ProtectedRemover and classA are unrelated.... 正如Mark B所提到的,Hovhannes Grigoryan提出的解决方案并不安全,可能会出现意外行为,因为ProtectedRemover和classA是无关的....

I used this in the past, and it may be safer I think: 我过去曾用过这个,我觉得它可能更安全:

class ClassA
{
protected:
   int varA; 
};

class ProtectedRemover : public ClassA // now, they are related!
{
public: // <- Note this! :)
   int getA() { return varA; }
   void setA( int a ) { varA = a; }
};

class ClassB
{
protected:
   ClassA objectA;

public: // Just add two methods below

   int getProtectedVarA()
   {
      return ((ProtectedRemover)*(&objectA))->getA();
   }

   void setProtectedVarA(int i)
   {
      ((ProtectedRemover)*(&objectA))->setA(i);
   }
};

Note it presents no risk at all to get undetermined behaviour....but may be less than original solution where ProtectedRemover and classA were unrelated....and also easier to do if ClassA has tones of attributes! 注意它没有任何风险来获得未确定的行为....但可能比ProtectedRemover和classA不相关的原始解决方案要少......如果ClassA具有属性的音调,也更容易做到!

Still not recommended unless you really have no other choice (can't modify classA by making your class friend or add settre/getter)! 除非你真的别无选择(不能通过让你的班级朋友修改classA或添加设置/获取者),仍然不推荐!

Nothing's really impossible.... 什么都不是真的不可能......

I assume modifying definition of ClassA is forbidden. 我认为禁止修改ClassA的定义。

Here is a tricky way for you, but I don't encouraging you to use it :) 这对你来说是一个棘手的方式,但我不鼓励你使用它:)

class ClassA
{
protected:
   int varA; 
};

class ProtectedRemover // magic thing
{
public: // <- Note this! :)
   int varA;
};

class ClassB
{
protected:
   ClassA objectA;

public: // Just add two methods below

   int getProtectedVarA()
   {
      return reinterpret_cast<ProtectedRemover*>(&objectA)->varA;
   }

   void setProtectedVarA(int i)
   {
      reinterpret_cast<ProtectedRemover*>(&objectA)->varA = i;
   }
};

int main()
{
   ClassB theMainObject;

   // Set protected thing.
   theMainObject.setProtectedVarA(3); 

   // Get protected thing.
   std::cout << theMainObject.getProtectedVarA() << std::endl;
}

So there is a way to access and modify protected/private data. 因此,有一种方法可以访问和修改受保护/私有数据。
Who was thinking it is impossible, vote up ;) 谁在想这是不可能的,投票;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM