data-members

Do you use Data Members or Public Properties from within the Class itself?

家住魔仙堡 提交于 2020-01-04 04:44:06
问题 If I have a simple class setup like this: class MyClass { private string _myName = string.Empty; public string MyName { get { return _myName; } } public void DoSomething() { // Get the name... string name = string.Empty; name = _myName; // OR name = MyName; // ...and do something with it... } } Which should I use, the public property, or the data member? Obviously, in this example it doesn't make a difference, since they both just reference the same variable. But what about real world uses of

Automatically-generated Python constructor

旧时模样 提交于 2019-12-10 19:34:09
问题 I have countless Python classes from various projects from SQLAlchemy (and a couple from Pygame as well), and I recently noticed a pattern in many of them: their constructors always went something like this: class Foo(Base): def __init__(self, first, last, email, mi=""): self.first = first self.last = last self.email = email self.mi = mi ... whereby the only thing the constructor did was to transfer a set of positional arguments into an exactly identically named set of data members,

Why isn't the const qualifier working on pointer members on const objects?

一曲冷凌霜 提交于 2019-12-01 16:56:41
I know this has been asked a lot, but the only answers I could find was when the const-ness was actually casted away using (int*) or similar. Why isn't the const qualifier working on pointer type member variables on const objects when no cast is involved? #include <iostream> class bar { public: void doit() { std::cout << " bar::doit() non-const\n"; } void doit() const { std::cout << " bar::doit() const\n"; } }; class foo { bar* mybar1; bar mybar2; public: foo() : mybar1(new bar) {} void doit() const { std::cout << "foo::doit() const\n"; std::cout << " calling mybar1->doit()\n"; mybar1->doit();

Why isn't the const qualifier working on pointer members on const objects?

巧了我就是萌 提交于 2019-12-01 15:27:47
问题 I know this has been asked a lot, but the only answers I could find was when the const-ness was actually casted away using (int*) or similar. Why isn't the const qualifier working on pointer type member variables on const objects when no cast is involved? #include <iostream> class bar { public: void doit() { std::cout << " bar::doit() non-const\n"; } void doit() const { std::cout << " bar::doit() const\n"; } }; class foo { bar* mybar1; bar mybar2; public: foo() : mybar1(new bar) {} void doit(

How to access private data members outside the class without making “friend”s? [duplicate]

一世执手 提交于 2019-11-26 11:15:01
问题 This question already has answers here : Can I access private members from outside the class without using friends? (24 answers) Closed 2 years ago . I have a class A as mentioned below:- class A{ int iData; }; I neither want to create member function nor inherit the above class A nor change the specifier of iData . My doubts:- How to access iData of an object say obj1 which is an instance of class A ? How to change or manipulate the iData of an object obj1 ? Note : Don\'t use friend . 回答1: