PC Review


Reply
Thread Tools Rate Thread

Calling a struct constructor in a class constructor body

 
 
Karl M
Guest
Posts: n/a
 
      18th Dec 2004
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

{

int a[5];

 
Reply With Quote
 
 
 
 
Carl Daniel [VC++ MVP]
Guest
Posts: n/a
 
      18th Dec 2004
Karl M wrote:
> 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:


Add an explicitly defined default constructor to your struct that does the
initialization:

>
> //Some.h


#include <algorithm>

> struct MyStruct
>
> {
> int a[5];


MyStruct()
{
std::fill(a,a+sizeof(a),0);
}
> };


HTH

-cd


 
Reply With Quote
 
Vladimir Nesterovsky
Guest
Posts: n/a
 
      18th Dec 2004
> Add an explicitly defined default constructor to your struct that does the
> initialization:
>
> >
> > //Some.h

>
> #include <algorithm>
>
> > struct MyStruct
> >
> > {
> > int a[5];

>
> MyStruct()
> {

std::fill(a,a+5,0);
> }
> > };

--
Vladimir Nesterovsky
e-mail: (E-Mail Removed)
home: http://www.nesterovsky-bros.com


 
Reply With Quote
 
Carl Daniel [VC++ MVP]
Guest
Posts: n/a
 
      19th Dec 2004
Vladimir Nesterovsky wrote:
> std::fill(a,a+5,0);


Yeah, that'd be better ;-) Or maybe even :

std::fill(a,a+sizeof(a)/sizeof(a[0]),0);

-cd


 
Reply With Quote
 
Karl M
Guest
Posts: n/a
 
      19th Dec 2004
Thank you guys,
See my new post regarding the struct ctor

Karl M


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Struct by Ref via Class Constructor Danny Tan Microsoft C# .NET 4 26th Aug 2005 05:42 PM
calling other constructor of same class Nikola Skoric Microsoft C# .NET 5 2nd Nov 2004 04:06 AM
Calling overloaded constructor with values inside another constructor Tristan Microsoft C# .NET 2 30th Mar 2004 01:05 PM
Calling Constructor from within the class. Jeff Woodie Microsoft C# .NET 3 21st Aug 2003 11:55 PM
Calling Constructor from within the class. Zoury Microsoft C# .NET 2 21st Aug 2003 11:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:25 PM.