 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Russell Hinds Guest
|
Posted: Mon Dec 22, 2003 3:12 pm Post subject: Using classes |
|
|
Can someone show me how to do the following, i am trying to learn about classes, i know its like a struct but a bit different.
How do you write a class, and initialize the class variables to null when you first call the class variables. Lets use this simple struct for example.
struct TNamesInfo
{
AnsiString Name;
AnsiString Address;
int Age;
};
// header
private:
TNamesInfo ni;
Then in the forms constructor you init the variables like.
ni.Name = "";
ni.Address = "";
ni.Age = 0;
How can i do the same procedure with a class? I think in a class, it has its own constructor where you can init the variables without using the forms constructor. Or am i confusing myself by thinking too technical.
I have seen in some structs, you will call the struct like
TNamesInfo *ni = new TNamesInfo; How come you can do it like that, and you can also use it like: ni.Name = "aname";
I am a bit lost on the usage of all of this, but i don't want to get too ahead of myself from the question i posted.
Appreciate the help.
Russell
|
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Mon Dec 22, 2003 5:08 pm Post subject: Re: Using classes |
|
|
Russell Hinds wrote:
| Quote: | How do you write a class, and initialize the class variables to null when you first call the class variables. Lets use this simple struct for example.
|
Please wrap your lines manually.
For what you are asking you don't have to do anything: all class or struct
variables will be zero when you create the struct/class with new.
| Quote: | I have seen in some structs, you will call the struct like
TNamesInfo *ni = new TNamesInfo; How come you can do it like that, and you can also use it like: ni.Name = "aname";
|
No you would use it like: ni->Name = "aname"; then.
Please tell if you want to initialise to something different as zero or empty.
Hans.
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Mon Dec 22, 2003 6:18 pm Post subject: Re: Using classes |
|
|
Hans Galema <dontusethis (AT) dontusethis (DOT) nl> wrote:
| Quote: | Russell Hinds wrote:
[...] and you can also use it like: ni.Name = "aname";
No you would use it like: ni->Name = "aname"; then.
|
That would depend how it's being referenced. If by a pointer,
then it would be ->. If referencing an instance that was
allocated on the stack:
TMyStruct MyStruct;
MyStruct.Name = "My Name";
~ JD
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Dec 22, 2003 6:49 pm Post subject: Re: Using classes |
|
|
"Hans Galema" <dontusethis (AT) dontusethis (DOT) nl> wrote
| Quote: | For what you are asking you don't have to do anything: all class
or struct variables will be zero when you create the struct/class
with new.
|
That is not true at all. The 'new' operator *does not* automatically zero
out the allocated memory. You have to do that yourself. You are probably
being confused by the fact that *VCL* classes automatically zero out their
own memory upon construction. But that is a VCL-specific feature, that is
not part of the C++ language itself at all.
Gambit
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Dec 22, 2003 6:55 pm Post subject: Re: Using classes |
|
|
"Russell Hinds" <cjhinds (AT) sympatic (DOT) uk> wrote
| Quote: | i am trying to learn about classes, i know its like a struct but a bit
different. |
As far as the compiler is concerned, a 'class' and a 'struct' are exactly
the same, except that the members of a 'struct' are 'public' by default if
you do not explitically declare 'public', whereas the members of a class are
'private' by default instead. Other than that, anything you can do with a
'class', you can do with a 'struct' and vice versa.
| Quote: | I think in a class, it has its own constructor where you can init the
variables without using the forms constructor.
|
Correct. And a 'struct' can have its own constructor as well.
struct TNamesInfo
{
AnsiString Name;
AnsiString Address;
int Age;
TNamesInfo()
{
Age = 0;
}
};
In the example above, AnsiString has its own constructor that initializes
its memory, so there is no need to set the strings to "" in your own code,
since it is already being done for you. You can alternatively use the
constructor's initialization list to initialize the 'int' member instead of
using an assginment statement inside the constructor body:
struct TNamesInfo
{
AnsiString Name;
AnsiString Address;
int Age;
TNamesInfo()
: Age(0)
{
}
};
| Quote: | I have seen in some structs, you will call the struct like
TNamesInfo *ni = new TNamesInfo; How come you can
do it like that, and you can also use it like: ni.Name = "aname";
|
You are invoking the default constructor in both cases. If you do not
explitically declare your own constructor, the compiler will create a
default one for you. A default constructor is simply one without any
parameters. In the 'new' statement above, you are creating the instance on
the heap, invoking the default constructor. If you had declared the
instance via the statement "TNamesInfo ni;" instead, the same constructor
will be invoked, but it will create the instance on the stack instead of the
heap.
Gambit
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Mon Dec 22, 2003 7:17 pm Post subject: Re: Using classes |
|
|
Hans Galema <dontusethis (AT) dontusethis (DOT) nl> wrote:
| Quote: | No. In the context you can see that ni is declared as a pointer.
|
We're both wrong - and right. One of his questions was exactly
this point - that he didn't understand when to use '->' v/s '.'.
His code showed allocation on both the heap AND the stack.
Russell, if you allocate on the stack:
TNamesInfo ni;
then you would use ni.Name = "Some Name";
If you allocate from the heap:
TNamesInfo *ni = new TNamesInfo;
then you would use ni->Name = "Some Name";
You could also (I don't know why you would but you can):
TNamesInfo ni;
TNamesInfo *pni = (TNamesInfo*)∋
and then these two lines are the same:
ni.Name = "Some Name";
pni->Name = "Some Name";
~ JD
|
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Mon Dec 22, 2003 8:05 pm Post subject: Re: Using classes |
|
|
JD wrote:
| Quote: | [...] and you can also use it like: ni.Name = "aname";
No you would use it like: ni->Name = "aname"; then.
That would depend how it's being referenced.
|
No. In the context you can see that ni is declared as a pointer.
Hans.
|
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Mon Dec 22, 2003 8:08 pm Post subject: Re: Using classes |
|
|
Remy Lebeau (TeamB) wrote:
| Quote: | For what you are asking you don't have to do anything: all class
or struct variables will be zero when you create the struct/class
with new.
That is not true at all. The 'new' operator *does not* automatically zero
out the allocated memory. You have to do that yourself. You are probably
being confused by the fact that *VCL* classes automatically zero out their
own memory upon construction. But that is a VCL-specific feature, that is
not part of the C++ language itself at all.
|
Aha. But it could also be a compiler feature.
Then until now i now I'm lucky seeing structures zeroed with new.
Hans..
|
|
| Back to top |
|
 |
Russell Hinds Guest
|
Posted: Mon Dec 22, 2003 9:08 pm Post subject: Re: Using classes |
|
|
| Quote: | Correct. And a 'struct' can have its own constructor as well.
struct TNamesInfo
{
AnsiString Name;
AnsiString Address;
int Age;
TTreeNode* Node;
TNamesInfo()
{
Age = 0;
Node = NULL;
}
};
|
But if i do my struct like that, do i have to use the 'new'
with it in order to initialize the constructor? Because i would much rather just use the TNamesInfo ni; instead of
having to create and delete the struct.
I just tried it, and i actually dont have to use it using the new statement. That is cool, i had not seen a struct like that
before. I tried it with a variable of TTreeNode and it
actually made it null, where before i had to null it out for
myself.
Thanks everyone for explaining this to me. I have one more
small question if you don't mind.
| Quote: | the heap, invoking the default constructor. If you had declared the
instance via the statement "TNamesInfo ni;" instead, the same constructor
will be invoked, but it will create the instance on the stack instead of the
heap.
|
What is the difference with working on the heap, or from the stack? I am totally lost on that one.
|
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Mon Dec 22, 2003 10:08 pm Post subject: Re: Using classes |
|
|
JD wrote:
| Quote: | Hans Galema <dontusethis (AT) dontusethis (DOT) nl> wrote:
No. In the context you can see that ni is declared as a pointer.
We're both wrong - and right.
|
No, there was nothing wrong in my statement.
| Quote: | TNamesInfo ni;
then you would use ni.Name = "Some Name";
If you allocate from the heap:
TNamesInfo *ni = new TNamesInfo;
then you would use ni->Name = "Some Name";
|
Correct.
Hans.
|
|
| Back to top |
|
 |
Duane Hebert Guest
|
Posted: Mon Dec 22, 2003 10:14 pm Post subject: Re: Using classes |
|
|
| Quote: | What is the difference with working on the heap, or from the stack? I am totally lost on that
one. |
When creating an instance on the heap, it's necessary to delete the pointer.
When creating it on the stack, it's gone when it goes out of scope.
|
|
| Back to top |
|
 |
Rodolfo Frino - Macrosoft Guest
|
Posted: Tue Dec 23, 2003 5:14 am Post subject: Re: Using classes |
|
|
Ther is another difference between struct and classes that you haven't
mentioned:
The default access specifier for a base class of derived struct is
public while for class is private.
Rodolfo
"Remy Lebeau (TeamB)" <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> wrote
| Quote: |
"Russell Hinds" <cjhinds (AT) sympatic (DOT) uk> wrote in message
news:3fe717f0$1 (AT) newsgroups (DOT) borland.com...
i am trying to learn about classes, i know its like a struct but a bit
different.
As far as the compiler is concerned, a 'class' and a 'struct' are exactly
the same, except that the members of a 'struct' are 'public' by default if
you do not explitically declare 'public', whereas the members of a class
are
'private' by default instead. Other than that, anything you can do with a
'class', you can do with a 'struct' and vice versa.
I think in a class, it has its own constructor where you can init the
variables without using the forms constructor.
Correct. And a 'struct' can have its own constructor as well.
struct TNamesInfo
{
AnsiString Name;
AnsiString Address;
int Age;
TNamesInfo()
{
Age = 0;
}
};
In the example above, AnsiString has its own constructor that initializes
its memory, so there is no need to set the strings to "" in your own code,
since it is already being done for you. You can alternatively use the
constructor's initialization list to initialize the 'int' member instead
of
using an assginment statement inside the constructor body:
struct TNamesInfo
{
AnsiString Name;
AnsiString Address;
int Age;
TNamesInfo()
: Age(0)
{
}
};
I have seen in some structs, you will call the struct like
TNamesInfo *ni = new TNamesInfo; How come you can
do it like that, and you can also use it like: ni.Name = "aname";
You are invoking the default constructor in both cases. If you do not
explitically declare your own constructor, the compiler will create a
default one for you. A default constructor is simply one without any
parameters. In the 'new' statement above, you are creating the instance
on
the heap, invoking the default constructor. If you had declared the
instance via the statement "TNamesInfo ni;" instead, the same constructor
will be invoked, but it will create the instance on the stack instead of
the
heap.
Gambit
|
|
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Tue Dec 23, 2003 9:20 am Post subject: Re: Using classes |
|
|
Hans Galema wrote:
| Quote: | Remy Lebeau (TeamB) wrote:
That is not true at all.
... until now i now I'm lucky seeing structures zeroed with new.
|
That was because after new I use initialisers like ZeroMemory().
Sorry for the wrong info.
Hans.
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Dec 23, 2003 7:30 pm Post subject: Re: Using classes |
|
|
"Hans Galema" <dontusethis (AT) dontusethis (DOT) nl> wrote
| Quote: | Aha. But it could also be a compiler feature.
|
Perhaps in theory, but in reality it is not.
| Quote: | Then until now i now I'm lucky seeing structures zeroed with new.
|
Correct. You should never have assumed that it would happen automatically.
Gambit
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Dec 23, 2003 7:35 pm Post subject: Re: Using classes |
|
|
"Russell Hinds" <cjhinds (AT) sympatic (DOT) uk> wrote
| Quote: | But if i do my struct like that, do i have to use the
'new' with it in order to initialize the constructor?
|
No. Just like a 'class' can invoke any constructor directly when allocating
an instance on the stack, the same applies to a 'struct' as well. For
example:
struct TNamesInfo
{
AnsiString Name;
AnsiString Address;
int Age;
TTreeNode* Node;
TNamesInfo(int Age)
{
this->Age = Age;
Node = NULL;
}
};
// on the heap
TNamesInfo *info = new TNamesInfo(10);
// on the stack
TNamesInfo info(10);
Gambit
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|