Need help from you guys about classes

S

Snake

Hi guys,
I have question about classes.
when u create class called Test.
and you define variable Test c;
so does this act like( a variable c of type Test pointing to an abject )?
The thing that I am confused with java where you say Test c = new Test; whre
you can move this Test object around by saying "Test b" and then b = c;(so c
and b points to the same object).
while in c++ I think it has different meaning(like b would have different
object cloned from c)?
so would explain me the difference
I hope that you understood what I ment in this confusing question.
Thanx alot for ytour efforts guys. I really apprecuate any help.
Regards,
 
A

Alf P. Steinbach

I have question about classes.
when u create class called Test.
and you define variable Test c;
so does this act like( a variable c of type Test pointing to an abject )?

No, it's a variable c that directly is a Test object.

The thing that I am confused with java where you say Test c = new Test; whre
you can move this Test object around by saying "Test b" and then b = c;(so c
and b points to the same object).
while in c++ I think it has different meaning(like b would have different
object cloned from c)?

Right, although in C++ "clone" usually implies creating a copy in
dynamically allocated memory.


so would explain me the difference

In Java variables of class types contain references to objects.

In C++ they contain objects directly.

C++ does not have a direct analog of Java references, but you can
use


A) Pointers.
Provides assignment like Java references, as well as more low-level
stuff like pointer arithmetic not present in Java.

B) References.
Provides transparent notation like Java references do (when c is a C++
reference the expression c refers directly to the object referred to by
c), but you cannot change what a reference refers to.

C) Smart-pointers.
E.g. std::auto_ptr or boost::shared_ptr. Provides automatic memory
management sort of like Java references (the main difference is that
most C++ smart-pointers use reference counting, which doesn't handle
circular data structures automatically), and typically also reference
assignment, but not transparent notation.


(C) is not a built-in feature of C++, it's just a way of using the language
mechanisms, such as template classes, to implement automatic memory management.
 
V

Victor Bazarov

Snake said:
I have question about classes.
when u create class called Test.
and you define variable Test c;
so does this act like( a variable c of type Test pointing to an abject )?

Rather, an identifier 'c' that designates an object of type Test.
The thing that I am confused with java where you say Test c = new Test; whre
you can move this Test object around by saying "Test b" and then b = c;(so c
and b points to the same object).
while in c++ I think it has different meaning(like b would have different
object cloned from c)?
Right.

so would explain me the difference

The difference is that in C++ there are three ways to create an object:
static, automatic, and dynamic. In Java... Well, you'll have to ask
in a Java newsgroup.

Static objects are either created outside of any function, or explicitly
declared "static". Automatic objects are created inside functions. And
dynamic objects are created in free store with the help of 'new' operator.

Example

class Test {}; // class Test defined

Test st; // static object

int main()
{
static Test stt; // another static object
Test at; // automatic object
Test *pdt = new Test; // a pointer to a dynamic object

delete pdt; // have to delete what you create dynamically
return 0;
}

Victor
 
S

Snake

Thanks alot guys for the help.That really clarified alot of thing.
But one more that I am not sure I really got.
In the example I mentiond(b = c).
do b and c each has spearated " identical" objects no matter how many
attributes are there?
ok,what if the object Test has a pointer d that refers to another different
object.
will b and c have two different pointers refering to the same object
referred by d previously or two pointers refereeing to two different object?
a bit confusing,eh!!
Thanx again, I really need help over this.
Regards
 
V

Victor Bazarov

Snake said:
Thanks alot guys for the help.That really clarified alot of thing.
But one more that I am not sure I really got.
In the example I mentiond(b = c).
do b and c each has spearated " identical" objects no matter how many
attributes are there?

Not necessarily. For all it's worth, b and c can be associated with
objects of two completely different classes, with the exception that
the b's type needs to define the assignment operator that accepts
the right side convertible from the c's type.

Or perhaps I just didn't understand the question...
ok,what if the object Test has a pointer d that refers to another different
object.
will b and c have two different pointers refering to the same object
referred by d previously or two pointers refereeing to two different
object?

Alright, so you say b and c are two different objects of the same type
that doesn't necessarily [re]define the assignment operator. And that
that type has a pointer as a member. Fine. During the assignment,
each member's assignment semantics will be followed (unless the operator=
is user-defined). For pointers that mean that after assignment is done,
they will point to the same object.

It can be circumvented, if you define your own assignment operator.

Example:

class Dummy {};
class NothingSpecial {
Dummy *p_;
public:
NothingSpecial(Dummy* p) : p_(p) {}
};

class OverloadedAssignment {
Dummy *p_;
public:
OverloadedAssignment(Dummy *p) : p_(p) {}
void operator =(OverloadedAssignment& oa) { } // p_ is left alone
};

int main() {
Dummy d1, d2;
NothingSpecial ns1(&d1), ns2(&d2); // ns1.p_ and ns2.p_ point to
// different Dummy objects
ns2 = ns1; // assignment semantics for pointers means now
// ns1.p_ and ns2.p_ point to the same object

OverloadedAssignment os1(&d1), os2(&d2); // point to different
os2 = os1; // overloaded assingment operator prevents copying
// of the p_ member -- they _STILL_ point to diff. obj
}

Victor
 
J

jeffc

Snake said:
Hi guys,
I have question about classes.
when u create class called Test.
and you define variable Test c;
so does this act like( a variable c of type Test pointing to an abject )?

No, it's a variable of type Type. It IS the object - it doesn't POINT to
the object.
The thing that I am confused with java where you say Test c = new Test; whre
you can move this Test object around by saying "Test b" and then b = c;(so c
and b points to the same object).

That's different - you used new. In that case you have a pointer.
while in c++ I think it has different meaning(like b would have different
object cloned from c)?

If you used "new" in C++, then you'd have a pointer too.
 
J

jeffc

Snake said:
Thanks alot guys for the help.That really clarified alot of thing.
But one more that I am not sure I really got.
In the example I mentiond(b = c).
do b and c each has spearated " identical" objects no matter how many
attributes are there?
ok,what if the object Test has a pointer d that refers to another different
object.
will b and c have two different pointers refering to the same object
referred by d previously or two pointers refereeing to two different object?
a bit confusing,eh!!

If b and c are objects of your classes, then it's up to you to define how
"=" works. If you don't define the "=" function yourself, then the compiler
gives you one, but it might not do what you want. If you want it to work so
that if the 2 pointers point to the same object then the original objects
are equal, then you should go ahead and write your "=" function to work that
way.
 
E

Evan Carew

Snake,

Just to make sure you are getting the pointer thing; in C++ when you use
the new() operator, it is exactly the same thing as in JAVA. That is to
say, you have created a reference to an object. Remember when in your
JAVA training they said that JAVA is a language which referes to
everything by reference? well that's what is going on here in C++ as
well. The difference in C++ is that you have even more options.

Evan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top