从静态成员函数访问私有非静态 class 变量 - C++
Accessing Private non-static class variable from Static member function - C++
私有非静态 variable/methods 可以在静态函数中访问吗?如是,
那么 "Private" 访问修饰符有什么用呢?请通过以下代码。
// Testclassheader.h file
class TestClass{
private:
int TestVariable; //Private Variable
int TestFunction(); //Private Method
public:
static void TeststaticFn(); //Static Method
};
void TestClass::TeststaticFn()
{
TestClass TestObj;
TestObj.TestVariable = 10; // Compiles perfectly
TestObj.TestFunction(); //Compiles Perfectly
}
// Another Class
//#include "Testclassheader.h"
class AnotherClass{
public:
int DummyFunc();
};
int AnotherClass::DummyFunc()
{
TestClass AnotherObj;
AnotherObj.TestVariable = 15; //error: 'TestVariable' is a private member of 'TestClass'
AnotherObj.TestFunction(); //error: 'TestFunction' is a private member of 'TestClass'
}
我在 Visual studio 12 中尝试了上面的代码。谁能解释为什么 private variable/methods 可以在静态方法中访问(实际上它不应该)?
Are private non static variable/methods accessible in static function?
是的,private non static variable/methods 可以通过属于同一 class.
的静态函数访问
If yes, then what is the use of "Private" access modifier?
它阻止其他 classes 访问 class 的私有 class 成员和私有实例成员 class。
Can anyone explain why private variable/methods are accessible in static method?
因为 class 的所有部分都是 class 的一部分。
which actually [private variable/methods] should not [be accessible to static methods of the same class])?
这是不正确的。
给定 class 的所有函数 都可以通过 any 访问该 class 的 private
成员实例。您似乎认为 private
限制对该特定实例的成员函数的访问,这是不正确的。
class foo
{
private:
int bar;
// access of the function doesn't matter, but let's use public
public:
// the only case you thought was legal:
void baz()
{
bar = 1;
}
// but this is perfectly legal
void qux(foo otherFoo)
{
otherFoo.bar = 1;
}
// also legal, as you discovered.
static void quux(foo iPittyTheFoo)
{
iPittyTheFoo.bar = 1;
}
};
class someOtherClass
{
// no function here (static or otherwise) has access to baz.
// UNLESS you "friend someOtherClass" inside class foo. Whether or not
// "friend" is ever a good idea is a matter of some debate.
};
void someGlobalFunction()
{
// Also cannot access bar.
Foo a;
a.bar = 1; // boom
}
// nope. Still cannot access bar.
foo b;
someOtherClass instance(b.bar); // boom
此外,“// throws error
”具有误导性。 "Throw" 专门指异常处理,而不是编译时错误。编译器错误、链接器错误和运行时错误各不相同,需要解决不同类型的问题来处理。当向不查看实际错误输出的人寻求帮助时,您需要指定它是哪一个。只是 copy-paste-ing 错误本身通常是个好主意,然后我们都有相同的信息。
总的来说,我怀疑您误解了public:
、protected:
和private:
的目的。我们都必须在某个时候学习它。
在C++中,public
functions/methods和variables/members(不同的人用不同的术语)代表一个class的"interface"。这些是 class 之外的所有内容都允许使用的内容。该界面背后发生的是他们业务的none(至少在理论上)。
protected
函数和变量可用于从 class 继承的 classes。 "Your version of this class may be customized in these ways".
private
函数和变量不是别人关心的。不敏感。随着程序的变化,给定 class 中的实现细节可能会有很大差异。 class 的初始实现可能(不寒而栗)return 硬编码值:
class X
{
...
private:
int Y() { return 1; }
};
同一函数的更高版本可能会在数据库中查找值,从文件中读取等等。 "Which database?" 好的,现在我们需要一个参数...
int Y(WhichDb thisOne) { return thisOne.lookupY(); }
所以现在调用 Y 的任何地方都需要传入一个 WhichDb(它可能应该是一个 const 引用,但这是一个完全不同的话题)。通过更改 Y 的 "function signature",我们破坏了所有调用 Y 的代码。换句话说,所有现有的对 Y 的调用现在都是编译器错误,因为它们没有传入 WhichDb。在某种意义上,public/protected/private 定义给定更改将 affect/break.
的代码量
私人的?就是那个class。没问题,我负责 class(因为我可以更改它 header),所以修复它没问题。
保护? class,加上从它继承的一切。这很容易破坏其他人的代码,这通常很糟糕。破坏您不负责的代码是失去客户的好方法。
Public?任何人、任何地方都可以调用该函数。应避免 "Breaking changes" 到 public 接口。
因此,也许您的 class 仅在您的公司内部、您的部门中由您使用过。 Public 那时的变化没什么大不了的。另一方面,Some Popular Library 确实做不到。我的意思是......他们可以,但他们可能会激怒很多人。
有多种方法可以在不破坏现有代码的情况下更改您的 public 界面。您可以添加新函数,可以向具有默认值的现有函数添加新参数:void foo(int bar = 2);
。调用 foo()
的人仍然会编译(并且希望仍然会得到他们所依赖的相同行为),但现在人们可以调用 foo(3)
来获得新的行为。
私有非静态 variable/methods 可以在静态函数中访问吗?如是, 那么 "Private" 访问修饰符有什么用呢?请通过以下代码。
// Testclassheader.h file
class TestClass{
private:
int TestVariable; //Private Variable
int TestFunction(); //Private Method
public:
static void TeststaticFn(); //Static Method
};
void TestClass::TeststaticFn()
{
TestClass TestObj;
TestObj.TestVariable = 10; // Compiles perfectly
TestObj.TestFunction(); //Compiles Perfectly
}
// Another Class
//#include "Testclassheader.h"
class AnotherClass{
public:
int DummyFunc();
};
int AnotherClass::DummyFunc()
{
TestClass AnotherObj;
AnotherObj.TestVariable = 15; //error: 'TestVariable' is a private member of 'TestClass'
AnotherObj.TestFunction(); //error: 'TestFunction' is a private member of 'TestClass'
}
我在 Visual studio 12 中尝试了上面的代码。谁能解释为什么 private variable/methods 可以在静态方法中访问(实际上它不应该)?
Are private non static variable/methods accessible in static function?
是的,private non static variable/methods 可以通过属于同一 class.
的静态函数访问If yes, then what is the use of "Private" access modifier?
它阻止其他 classes 访问 class 的私有 class 成员和私有实例成员 class。
Can anyone explain why private variable/methods are accessible in static method?
因为 class 的所有部分都是 class 的一部分。
which actually [private variable/methods] should not [be accessible to static methods of the same class])?
这是不正确的。
给定 class 的所有函数 都可以通过 any 访问该 class 的 private
成员实例。您似乎认为 private
限制对该特定实例的成员函数的访问,这是不正确的。
class foo
{
private:
int bar;
// access of the function doesn't matter, but let's use public
public:
// the only case you thought was legal:
void baz()
{
bar = 1;
}
// but this is perfectly legal
void qux(foo otherFoo)
{
otherFoo.bar = 1;
}
// also legal, as you discovered.
static void quux(foo iPittyTheFoo)
{
iPittyTheFoo.bar = 1;
}
};
class someOtherClass
{
// no function here (static or otherwise) has access to baz.
// UNLESS you "friend someOtherClass" inside class foo. Whether or not
// "friend" is ever a good idea is a matter of some debate.
};
void someGlobalFunction()
{
// Also cannot access bar.
Foo a;
a.bar = 1; // boom
}
// nope. Still cannot access bar.
foo b;
someOtherClass instance(b.bar); // boom
此外,“// throws error
”具有误导性。 "Throw" 专门指异常处理,而不是编译时错误。编译器错误、链接器错误和运行时错误各不相同,需要解决不同类型的问题来处理。当向不查看实际错误输出的人寻求帮助时,您需要指定它是哪一个。只是 copy-paste-ing 错误本身通常是个好主意,然后我们都有相同的信息。
总的来说,我怀疑您误解了public:
、protected:
和private:
的目的。我们都必须在某个时候学习它。
在C++中,public
functions/methods和variables/members(不同的人用不同的术语)代表一个class的"interface"。这些是 class 之外的所有内容都允许使用的内容。该界面背后发生的是他们业务的none(至少在理论上)。
protected
函数和变量可用于从 class 继承的 classes。 "Your version of this class may be customized in these ways".
private
函数和变量不是别人关心的。不敏感。随着程序的变化,给定 class 中的实现细节可能会有很大差异。 class 的初始实现可能(不寒而栗)return 硬编码值:
class X
{
...
private:
int Y() { return 1; }
};
同一函数的更高版本可能会在数据库中查找值,从文件中读取等等。 "Which database?" 好的,现在我们需要一个参数...
int Y(WhichDb thisOne) { return thisOne.lookupY(); }
所以现在调用 Y 的任何地方都需要传入一个 WhichDb(它可能应该是一个 const 引用,但这是一个完全不同的话题)。通过更改 Y 的 "function signature",我们破坏了所有调用 Y 的代码。换句话说,所有现有的对 Y 的调用现在都是编译器错误,因为它们没有传入 WhichDb。在某种意义上,public/protected/private 定义给定更改将 affect/break.
的代码量私人的?就是那个class。没问题,我负责 class(因为我可以更改它 header),所以修复它没问题。
保护? class,加上从它继承的一切。这很容易破坏其他人的代码,这通常很糟糕。破坏您不负责的代码是失去客户的好方法。
Public?任何人、任何地方都可以调用该函数。应避免 "Breaking changes" 到 public 接口。
因此,也许您的 class 仅在您的公司内部、您的部门中由您使用过。 Public 那时的变化没什么大不了的。另一方面,Some Popular Library 确实做不到。我的意思是......他们可以,但他们可能会激怒很多人。
有多种方法可以在不破坏现有代码的情况下更改您的 public 界面。您可以添加新函数,可以向具有默认值的现有函数添加新参数:void foo(int bar = 2);
。调用 foo()
的人仍然会编译(并且希望仍然会得到他们所依赖的相同行为),但现在人们可以调用 foo(3)
来获得新的行为。