newbie question on classes
if i were to make a class with a var in it what is the best way for code outside the object scope to access this var
//like this:
class test {
public int m_int;
} test m_test;
void somevoid () {
m_test.m_int = 2;
}
//or more like this
class test2 {
public:
int m_int;
void accessInt(int val) { m_int = val;};
} test2 m_test2
void somevoid () {
m_test2.accessInt(20);
}
are there more ways then this?, would anything change if the test or test2 objects were being accessed inderectly test* test2*
thanks kenny;
Hey, the best way to allow access to the variable is to have access functions that would check the new value to be sure it is valid within the variables purpose.
Brent Robinson
"What if this is as good as it gets?"
Brent Robinson
"What if this is as good as it gets?"
"The computer programmer is a creator of universes for which he alone is the lawgiver...No playwright, no stage director, no emperor, however powerful, has ever exercised such absolute athority to arrange a stage or a field of battle and to command such unswervingly dutiful actors or troops." - Joseph Weizenbaum-Brent Robinson
well, in classes you can access all variables publically or privately. within these sets, the rest is up to you. (sort of)
JoeMont001@aol.com www.polarisoft.n3.net
JoeMont001@aol.com www.polarisoft.n3.net
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
You should never have public member data in your classes. This breaks the rule of encapsulation (OOP Conformedness Rule No.454.4.6). You should always have private or protected access for data members and access them through functions. It is safer to do.
-=[ Lucas ]=-
-=[ Lucas ]=-
-=[ Lucas ]=-
quote: You should never have public member data in your classes. This breaks the rule of encapsulation (OOP Conformedness Rule No.454.4.6).
In some cases, it''s just a pain in the ass to make everything private/protected, and then need a function to access all the private data. Who cares if you don''t follow that rule? Are the "Code Police" going to bust down my door and bust me? No. Just do what''s the easiest, not what follows someones rules.
=======================================
A man with no head is still a man.
A head with no man is plain freaky.
In my experience, if you are creating a very complex system, public member variables on classes is a shot in the foot, it leads to really lazy programming. If you are going to do this, then you may as well use structs, and not classes.
Then again, going to town on OOP is not always a good idea either, try to find a happy medium between OOP and a functional aproach. Also, use the friend keyword if it helps. This means you can declare classes and function as being able to have public access to otherwise private or protected members of objects like so:
class MyClass2; // late binding prototype
class MyClass {
public:
void DoYourStuff(MyClass2 * mc2) {
mc2->value = 69;
}
}
class MyClass2 {
friend class MyClass;
private:
long value;
}
Edited by - NBGH on September 18, 2000 10:34:08 AM
Then again, going to town on OOP is not always a good idea either, try to find a happy medium between OOP and a functional aproach. Also, use the friend keyword if it helps. This means you can declare classes and function as being able to have public access to otherwise private or protected members of objects like so:
class MyClass2; // late binding prototype
class MyClass {
public:
void DoYourStuff(MyClass2 * mc2) {
mc2->value = 69;
}
}
class MyClass2 {
friend class MyClass;
private:
long value;
}
Edited by - NBGH on September 18, 2000 10:34:08 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement