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