Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 31st, 2005, 06:15 PM
Vince
Guest
 
Posts: n/a
Default struct and constructor

I recently learned that it's possible to put a constructor inside a struct.

My question is : Is it possible to do the following :

typedef struct _TRecInfo
{
_TRecInfo(int nKey, int nMode): nKey(nKey), nMode(nMode){}; //constructor

int nKey;
int nMode;
} TRecInfo;


and after something like :

TRecInfo recInfo[255];
recInfo[0x17] = new TRecInfo(0x0E, 1);


  #2  
Old August 31st, 2005, 06:25 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: struct and constructor

Vince wrote:[color=blue]
> I recently learned that it's possible to put a constructor inside a struct.
>
> My question is : Is it possible to do the following :
>
> typedef struct _TRecInfo[/color]

Technically speaking this is not allowed. Identifiers that begin with
an underscore and a capital letter are reserved by the implementation.
[color=blue]
> {
> _TRecInfo(int nKey, int nMode): nKey(nKey), nMode(nMode){}; //constructor
>
> int nKey;
> int nMode;
> } TRecInfo;
>
>
> and after something like :
>
> TRecInfo recInfo[255];
> recInfo[0x17] = new TRecInfo(0x0E, 1);[/color]

Yes. But why would you want to? Why not simply write

struct TRecInfo {

and proceed from there?

V
  #3  
Old August 31st, 2005, 06:35 PM
mlimber
Guest
 
Posts: n/a
Default Re: struct and constructor

Vince wrote:[color=blue]
> I recently learned that it's possible to put a constructor inside a struct.[/color]

Correct. structs differ from classes only in that their default is
public rather than private access.
[color=blue]
> My question is : Is it possible to do the following :
>
> typedef struct _TRecInfo
> {
> _TRecInfo(int nKey, int nMode): nKey(nKey), nMode(nMode){}; //constructor
>
> int nKey;
> int nMode;
> } TRecInfo;[/color]

Yes, but more common notation would be:

struct TRecInfo
{
TRecInfo( int nKey, int nMode )
: nKey_(nKey), nMode_(nMode)
{} // no semicolon necessary

int nKey_;
int nMode_;
};

You might even make the data private and provide accessor methods,
depending on what the class does. Anyway, the typedef is superfluous
because in C++ you can still refer to that struct as simply "TRecInfo"
(no "struct" keyword necessary).
[color=blue]
> and after something like :
>
> TRecInfo recInfo[255];
> recInfo[0x17] = new TRecInfo(0x0E, 1);[/color]

Presumably you meant someting like:

TRecInfo* records[ 255 ];
records[ 0x17 ] = new TRecInfo( 0xe, 1 );

The syntax you used would not work because the first line would call an
implicit default constructor for each element in the array (and you'd
get an error because TRecInfo::TRecInfo(void) doesn't exist) and
because the second line would be unable to find a conversion from
TRecInfo (the left-hand side) to TRecInfo* (the right-hand side).

If you want an array of these, consider using std::vector instead of
manually allocating an array yourself:

#include <vector>

// ...

void Foo()
{
std::vector<TRecInfo> records( 255, TRecInfo(0,0) );
// ...
}

For more on constructors, see these FAQs:

http://www.parashift.com/c++-faq-lite/ctors.html

Cheers! --M

  #4  
Old August 31st, 2005, 06:45 PM
red floyd
Guest
 
Posts: n/a
Default Re: struct and constructor

Vince wrote:[color=blue]
> I recently learned that it's possible to put a constructor inside a struct.
>
> My question is : Is it possible to do the following :
>
> typedef struct _TRecInfo
> {
> _TRecInfo(int nKey, int nMode): nKey(nKey), nMode(nMode){}; //constructor
>
> int nKey;
> int nMode;
> } TRecInfo;
>
>
> and after something like :
>
> TRecInfo recInfo[255];[/color]
This will blow up. YOu have no default constructor.
[color=blue]
> recInfo[0x17] = new TRecInfo(0x0E, 1);[/color]

recInfo[0x17] = TRecInfo(0x0e, 1);[color=blue]
>[/color]
  #5  
Old August 31st, 2005, 06:45 PM
Andre Kostur
Guest
 
Posts: n/a
Default Re: struct and constructor

Victor Bazarov <v.Abazarov@comAcast.net> wrote in
news:9wlRe.31446$Tf5.26224@newsread1.mlpsca01.us.t o.verio.net:
[color=blue]
> Vince wrote:[color=green]
>> I recently learned that it's possible to put a constructor inside a
>> struct.
>>
>> My question is : Is it possible to do the following :
>>
>> typedef struct _TRecInfo[/color]
>
> Technically speaking this is not allowed. Identifiers that begin with
> an underscore and a capital letter are reserved by the implementation.
>[color=green]
>> {
>> _TRecInfo(int nKey, int nMode): nKey(nKey), nMode(nMode){};
>> //constructor
>>
>> int nKey;
>> int nMode;
>> } TRecInfo;
>>
>>
>> and after something like :
>>
>> TRecInfo recInfo[255];
>> recInfo[0x17] = new TRecInfo(0x0E, 1);[/color]
>
> Yes. But why would you want to? Why not simply write
>
> struct TRecInfo {
>
> and proceed from there?[/color]

And... recInfo[0x17] is of type TRecInfo, and not TRecInfo* ... so why
new TRecInfo?
  #6  
Old August 31st, 2005, 11:45 PM
Vince
Guest
 
Posts: n/a
Default Re: struct and constructor

mlimber a écrit :[color=blue]
> Vince wrote:
>[color=green]
>>I recently learned that it's possible to put a constructor inside a struct.[/color]
>
>
> Correct. structs differ from classes only in that their default is
> public rather than private access.
>
>[color=green]
>>My question is : Is it possible to do the following :
>>
>>typedef struct _TRecInfo
>>{
>>_TRecInfo(int nKey, int nMode): nKey(nKey), nMode(nMode){}; //constructor
>>
>>int nKey;
>>int nMode;
>>} TRecInfo;[/color]
>
>
> Yes, but more common notation would be:
>
> struct TRecInfo
> {
> TRecInfo( int nKey, int nMode )
> : nKey_(nKey), nMode_(nMode)
> {} // no semicolon necessary
>
> int nKey_;
> int nMode_;
> };
>
> You might even make the data private and provide accessor methods,
> depending on what the class does. Anyway, the typedef is superfluous
> because in C++ you can still refer to that struct as simply "TRecInfo"
> (no "struct" keyword necessary).
>
>[color=green]
>>and after something like :
>>
>>TRecInfo recInfo[255];
>>recInfo[0x17] = new TRecInfo(0x0E, 1);[/color]
>
>
> Presumably you meant someting like:
>
> TRecInfo* records[ 255 ];
> records[ 0x17 ] = new TRecInfo( 0xe, 1 );
>
> The syntax you used would not work because the first line would call an
> implicit default constructor for each element in the array (and you'd
> get an error because TRecInfo::TRecInfo(void) doesn't exist) and
> because the second line would be unable to find a conversion from
> TRecInfo (the left-hand side) to TRecInfo* (the right-hand side).
>
> If you want an array of these, consider using std::vector instead of
> manually allocating an array yourself:
>
> #include <vector>
>
> // ...
>
> void Foo()
> {
> std::vector<TRecInfo> records( 255, TRecInfo(0,0) );
> // ...
> }
>
> For more on constructors, see these FAQs:
>
> http://www.parashift.com/c++-faq-lite/ctors.html
>
> Cheers! --M
>[/color]
Do I need to call delete after ?
Because I am initializing this struct array in my constructor.

CCardReader::CCardReader()
{
recInfo[0x17] = new TRecInfo(0x0E, 1);
recInfo[0x18] = new TRecInfo(0x12, 1);
...

}

CCardReader::~CCardReader()
{
???
}

  #7  
Old September 1st, 2005, 09:05 AM
Karl Heinz Buchegger
Guest
 
Posts: n/a
Default Re: struct and constructor

Vince wrote:[color=blue]
>
> Do I need to call delete after ?
> Because I am initializing this struct array in my constructor.
>
> CCardReader::CCardReader()
> {
> recInfo[0x17] = new TRecInfo(0x0E, 1);
> recInfo[0x18] = new TRecInfo(0x12, 1);
> ...
>
> }
>
> CCardReader::~CCardReader()
> {
> ???
> }[/color]

Yes. You need to delete. The rule is very simple:
for every executed new, there must be a corresponding
delete executed. Otherwise you leak memory.

So, look in the above: You use new. Thus there must be a
delete somewhere.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
  #8  
Old September 1st, 2005, 01:45 PM
mlimber
Guest
 
Posts: n/a
Default Re: struct and constructor

Vince wrote:[color=blue]
> Do I need to call delete after ?
> Because I am initializing this struct array in my constructor.
>
> CCardReader::CCardReader()
> {
> recInfo[0x17] = new TRecInfo(0x0E, 1);
> recInfo[0x18] = new TRecInfo(0x12, 1);
> ...
>
> }
>
> CCardReader::~CCardReader()
> {
> ???
> }[/color]

Yes. BUT it is preferable to avoid the use of new and delete if you can
by using standard containers like std::vector, which will handle the
dynamic memory allocation and deallocation for you. If you do need to
new and delete, you should always attach the memory to a smart pointer
(e.g., std::auto_ptr, boost::scoped_ptr, boost::shared_ptr) that will
automatically clean up after you. These techniques will virtually
eliminate opportunity for memory leaks in most programs, according to
Sutter and Alexandrescu's _C++ Coding Standards_. Boost-like smart
pointers will be released in the technical report on the standard C++
library (aka, TR1) and will likely be part of the forthcoming C++0x
update to the language and standard libraries.

For more on new and delete, see these FAQs
(http://www.parashift.com/c++-faq-lit...ore-mgmt.html), and for
more on smart pointers, check out Scott Meyers' _More Effective C++_,
Item 28; Alexandrescu's _Modern C++ Design_, chapter 7 (online for free
at http://www.informit.com/articles/art...redir=1&rl=1);
and the Boost smart pointer library documentation
(http://boost.org/libs/smart_ptr/smart_ptr.htm).

Cheers! --M

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles