473,770 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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);
Aug 31 '05 #1
7 28840
Vince wrote:
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
Aug 31 '05 #2
Vince wrote:
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::TRecI nfo(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<TRe cInfo> records( 255, TRecInfo(0,0) );
// ...
}

For more on constructors, see these FAQs:

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

Cheers! --M

Aug 31 '05 #3
Vince wrote:
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);

Aug 31 '05 #4
Victor Bazarov <v.********@com Acast.net> wrote in
news:9w******** ***********@new sread1.mlpsca01 .us.to.verio.ne t:
Vince wrote:
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?
Aug 31 '05 #5
mlimber a écrit :
Vince wrote:
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(i nt 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::TRecI nfo(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<TRe cInfo> 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::CC ardReader()
{
recInfo[0x17] = new TRecInfo(0x0E, 1);
recInfo[0x18] = new TRecInfo(0x12, 1);
...

}

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

Aug 31 '05 #6
Vince wrote:

Do I need to call delete after ?
Because I am initializing this struct array in my constructor.

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

}

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


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
kb******@gascad .at
Sep 1 '05 #7
Vince wrote:
Do I need to call delete after ?
Because I am initializing this struct array in my constructor.

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

}

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


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_p tr, boost::shared_p tr) 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

Sep 1 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2121
by: Peter van der Goes | last post by:
When a struct is created in C# and a parameterized constructor defined, the IntelliSense editor shows only the syntax for the parameterized constructor in tooltips, but not for the default constructor which appears to still exist. If this is not designed behavior, is it a bug or am I misusing my struct? If it is by design, could you please explain? I realize the behavior of a struct, as regards constructors, is different from that of...
8
5974
by: slurper | last post by:
hi, i'm studying some stl. i saw the pair implementation in a header-file but what i wonder is if a struct can have constructors as it seems in following snippet from the stl library. the snippets i wonder about are indicated with --------> : i thought a struct doesn't have constructors (but here there are four, actually three) template <class _T1, class _T2> struct pair {
4
1284
by: Karl M | last post by:
Hi C++ experts! Definitely I skipped C/C++ 101 because I have this pitfall: I need to call the struct ctor in the class default ctor body see below: //Some.h struct MyStruct
74
16036
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.