Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / December 2004

Tip: Looking for answers? Try searching our database.

Calling a struct constructor in a class constructor body

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Karl M - 18 Dec 2004 10:39 GMT
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];

           .

};

class MyClass

{

public:

           MyClass();

~MyClass();

           .

protected:

           .

           MyStruct m_MyStruct;

           .

};

//some.cpp

#include "some.h"

MyClass::MyClass() : m_MyStruct()

// default ctor of the struct with NULL- it works but not with the order of
class member declarations I have, so...

{

           // here I need to have m_MyStruct() initialized with NULL not
using m_MyStruct.a[1] = 0; etc .

           //How? Or maybe.m_MyStruct = MyStruct(); I this correct?

}

It's painful, now I understand VB programmers - easy street :)

Thank you,

Karl M
Carl Daniel [VC++ MVP] - 18 Dec 2004 15:06 GMT
> 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
Vladimir Nesterovsky - 18 Dec 2004 16:52 GMT
> Add an explicitly defined default constructor to your struct that does the
> initialization:
[quoted text clipped - 10 lines]
>         MyStruct()
>         {
             std::fill(a,a+5,0);
>         }
> > };
--
Vladimir Nesterovsky
e-mail: vladimir@nesterovsky-bros.com
home: http://www.nesterovsky-bros.com
Carl Daniel [VC++ MVP] - 19 Dec 2004 03:32 GMT
>              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
Karl M - 19 Dec 2004 13:21 GMT
Thank you guys,
See my new post regarding the struct ctor

Karl M

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.