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); 7 28814
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
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::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
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);
Victor Bazarov <v.********@comAcast.net> wrote in
news:9w*******************@newsread1.mlpsca01.us.t o.verio.net: 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?
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(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()
{
???
}
Vince wrote: 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.
--
Karl Heinz Buchegger kb******@gascad.at
Vince wrote: 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-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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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
|
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...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |