Skip to main content
gamedev.net
🔒 Locked

Simple C++ vs. Java question - public data members

Started by UserX03 Jun 14, 2002 at 12:24 AM 6 replies 400+ views
Original Post
UserX03
UserX03
Here is a simple C++ vs. Java question. In Java, why is it that two different classes can access each others'' public data members (without creating a class object), whereas in (Visual) C++, you get a compile error? I seem to recall that doing this in C++ is allowed just as it is in Java, but obviously the compile error says otherwise. for example in Java, this is allowed: class MyClassA { public int variableA = 1; ... } class MyClassB { ... void function() { int variableB = MyClassA.variableA; } } but in C++, this isn''t allowed ("illegal use of this type as an expression"): class MyClassA { public: int variableA; ... } class MyClassB { public: int variableB; ... } void MyClassB::function() { variableB = MyClassA.variableA; }
Null and Void
Null and Void
Replace MyClassA.variableA with AnInstanceOfMyClassA.variableA.

IndirectX
IndirectX
That would be allowed if you make those variables static.
---visit #directxdev on afternet <- not just for directx, despite the name
Null and Void
Null and Void
quote:
Original post by IndirectX
That would be allowed if you make those variables static.

No it wouldn''t. That would be MyClassA::variableA.

Ziphnor
Ziphnor
quote:
In Java, why is it that two different classes can access each others'' public data members (without creating a class object),


You cant do that in java(at least not without creating an instance of MyClassA and accessing that), not unless the public data member happens to be static too, and in that case you can do it in C++ too.(with the syntax CLASSNAME::STATICDATAMEMBER).
CaptainJester
CaptainJester
quote:
Original post by Ziphnor
You cant do that in java(at least not without creating an instance of MyClassA and accessing that), not unless the public data member happens to be static too, and in that case you can do it in C++ too.(with the syntax CLASSNAME::STATICDATAMEMBER).


You are correct, but in Java static data members are accessed using dot notation, not scope notation. That is where the confusion lies.

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
jjmontes
jjmontes
In Java this is correct:

ClassA.StaticMethod ();
ClassA.StaticVariable ();

If they are static members. In C++ this is done with:

ClassA::StaticMethod();
ClassA::StaticVariable();

And of course, if you have an instance of the class you can access its members as usual.

Real programers are not afraid from maths! (but I am)
JJpRiVaTe (Private Zone)
-=[ J ]=-I always forget to change the tagline.
Grib
Grib
In C++ you could do this:

class myA {
public:
int varA;
myA() : varA(1) {}
};

class myB {
int varB;
myB() : varB(myA().varA) {}
};


It''s kind of ugly and requires a temporary, it also does not document the dependence that myB has on myA anywhere in myA. I think a protected/private const static int in myA and making myB a friend would be the better way to go.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.