struct and constructor

V

Vince

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);
 
V

Victor Bazarov

Vince said:
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

Technically speaking this is not allowed. Identifiers that begin with
an underscore and a capital letter are reserved by the implementation.
{
_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);

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

struct TRecInfo {

and proceed from there?

V
 
A

Andre Kostur

Vince said:
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

Technically speaking this is not allowed. Identifiers that begin with
an underscore and a capital letter are reserved by the implementation.
{
_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);

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

struct TRecInfo {

and proceed from there?

And... recInfo[0x17] is of type TRecInfo, and not TRecInfo* ... so why
new TRecInfo?
 
M

mlimber

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

Correct. structs differ from classes only in that their default is
public rather than private access.
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;

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).
and after something like :

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

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
 
R

red floyd

Vince said:
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];
This will blow up. YOu have no default constructor.
recInfo[0x17] = new TRecInfo(0x0E, 1);

recInfo[0x17] = TRecInfo(0x0e, 1);
 
V

Vince

mlimber a écrit :
Vince said:
I recently learned that it's possible to put a constructor inside a struct.


Correct. structs differ from classes only in that their default is
public rather than private access.

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;


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).

and after something like :

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


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
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()
{
???
}
 
K

Karl Heinz Buchegger

Vince said:
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()
{
???
}

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.
 
M

mlimber

Vince said:
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()
{
???
}

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-lite/freestore-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/article.asp?p=25264&redir=1&rl=1);
and the Boost smart pointer library documentation
(http://boost.org/libs/smart_ptr/smart_ptr.htm).

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top