469,963 Members | 1,924 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

are objs of classes and structs refrence types??

hi ,

i'm a C / C# programmer .. have'nt done C++,

in C# .. . object instances of classes ( TextBox txt = new TextBox();)
are reference types. Structs on the other hand are value types.

In C++ i knw there are a few difference between classes and structs but
i need to know if there are value or refrence types.

Thanz

Gideon

Dec 31 '06 #1
5 1410
On 2006-12-31 13:35, giddy wrote:
hi ,

i'm a C / C# programmer .. have'nt done C++,

in C# .. . object instances of classes ( TextBox txt = new TextBox();)
are reference types. Structs on the other hand are value types.

In C++ i knw there are a few difference between classes and structs but
i need to know if there are value or refrence types.
They are what you want them to be, you can have them as value, reference
and pointer. What you use will depend on the situation.

The following is a short demonstration:

class Test
{
int a;
public:
Test() : a(0) { }
};

int main()
{
// Create as value (on stack), notice no ()
Test test1;

// test2 is a reference to test1, notice the &
Test& test2 = test1;

// Create an instance of Test on the heap and make
// test3 a pointer to that instance
Test* test3 = new Test();

// Make test4 a reference to test3
Test& test4 = *test3;

}

I'd advice you to find a good book and read up on the differences and
usages of the different types. As an example it's often a good idea to
pass classes/structs as referencest to functions:

int foo(Test& t)
{
// Do something
}

If possible avoid using pointers, but as you'll discover it's not always
possible.

The difference between class and struct is that by default a member of a
struct is public but a member of a class is private.

--
Erik Wikström
Dec 31 '06 #2
giddy wrote:
hi ,

i'm a C / C# programmer .. have'nt done C++,

in C# .. . object instances of classes ( TextBox txt = new TextBox();)
are reference types. Structs on the other hand are value types.

In C++ i knw there are a few difference between classes and structs but
i need to know if there are value or refrence types.
By default, value types. Whenever you want a reference, you
need to explicitly tell the compiler using the '&' symbol.

The only difference between a class and a struct is the one
Erik already told you.

HTH,
- J.
Dec 31 '06 #3
?- C++ ?? ?? ????? ?? ??????? new ??? ?? ?????? ???? ??? reference types, ??
???? ??, ??? ???? ????? ????????? ?? ?-stack ?? ??? new ??? ?? ?-heap

???? ????!

I know I can't use this language, so please forgive me
"giddy" <gi*******@gmail.comwrote in message
news:11**********************@i12g2000cwa.googlegr oups.com...
hi ,

i'm a C / C# programmer .. have'nt done C++,

in C# .. . object instances of classes ( TextBox txt = new TextBox();)
are reference types. Structs on the other hand are value types.

In C++ i knw there are a few difference between classes and structs but
i need to know if there are value or refrence types.

Thanz

Gideon


Dec 31 '06 #4
Mari wrote:
?- C++ ?? ?? ????? ?? ??????? new ??? ?? ?????? ???? ??? reference types, ??
???? ??, ??? ???? ????? ????????? ?? ?-stack ?? ??? new ??? ?? ?-heap

???? ????!

I know I can't use this language, so please forgive me
Whatever language you were trying to post in; it didn't work.

--
Clark S. Cox III
cl*******@gmail.com
Jan 1 '07 #5
hey ,

thanks Erik , its going to be a while now before i learn C++ ,
personally i LOVE C# and im gonna get grounded in it first. (i know C ,
Vb and i've done a bunch of other languages too =) ) I just wanted to
know a little bit ABOUT C++.
Jakek wrote :By default, value types.

Yep , this is exactly what i wanted.I should have specified BY DEFAULT.

Thanks

Gideon

Jan 1 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by nick | last post: by
4 posts views Thread by Christopher Ireland | last post: by
10 posts views Thread by Ashish Sheth | last post: by
5 posts views Thread by Bilgehan.Balban | last post: by
2 posts views Thread by thomasfarrow | last post: by
29 posts views Thread by Dom | last post: by
19 posts views Thread by desktop | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.