By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,563 Members | 1,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,563 IT Pros & Developers. It's quick & easy.

Need help from you guys about classes

P: n/a
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,
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Tue, 20 Jan 2004 01:30:59 GMT, "Snake" <ha***@rogers.com> wrote:
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.

Jul 22 '05 #2

P: n/a
"Snake" <ha***@rogers.com> wrote...
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
Jul 22 '05 #3

P: n/a
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

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:_L%Ob.87988$5V2.135777@attbi_s53...
"Snake" <ha***@rogers.com> wrote...
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

Jul 22 '05 #4

P: n/a
"Snake" <ha***@rogers.com> wrote...
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
Jul 22 '05 #5

P: n/a

"Snake" <ha***@rogers.com> wrote in message
news:nx******************@news04.bloor.is.net.cabl e.rogers.com...
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.
Jul 22 '05 #6

P: n/a

"Snake" <ha***@rogers.com> wrote in message
news:XY********************@twister01.bloor.is.net .cable.rogers.com...
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.
Jul 22 '05 #7

P: n/a
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

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.