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 / C++ Libraries / 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 13:21 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];
   //etc
};
class MyClass
{
public:
   MyClass();
   ~MyClass();
   //etc
protected:
   //etc
   MyStruct m_MyStruct;
   //etc
};

//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(); Is this correct? It init NULL
m_MyStruct
but uses a local stack var MyStruct() - stupid*/
}

Thank you
Karl
Ashok K Kumar - 23 Dec 2004 13:00 GMT
Hi,

It not clear what you wish to achieve. But Let me take a guess. You want to
initialize all the members of the struct to 0. There are two ways.
       a) Have a ctor for MyStruct and initialize it 0. This will
initialize all the members to null when an object of the struct is created.
               struct MyStruct
               {
                   int a[5];
                   //other members
                   //etc.....

                   //stuct ctor
                   MyStruct()
                   {
                           ZeroMemory(this,sizeof(MyStruct)); // or use
memset
                   }
               };
       b) Call Zeromemory or memset in the ctor of the class to set all the
members of the struct to null
                   MyClass::MyClass()
                   {
                           ZeroMemory(&m_MyStruct, sizeof(MyStruct));
                   }

HTH
Signature

_____________________
Ashok K Kumar
ashokkal at gmail dot com

> Hi C++ experts!
> Definitely I skipped C/C++ 101 because I have this pitfall:
[quoted text clipped - 34 lines]
> Thank you
> Karl
Karl M - 26 Dec 2004 03:37 GMT
Hi Ashok,
I guess I was not very explicit with my message:
I was hoping that there is a way to call the "default" constructor for a
structure inside the class constructor body.
Again, if you don't define a constructor for a struct and define the class
constructor like:
MyClass::MyClass() : m_MyStruct() {}
the compiler generate the underlying struct ctor code that init with NULL
the struct members not using any RTL ZeroMemory or any other calls but just
using push 5; pop ecx; rep stos [m_MyStruct].
Regards,
Karl

> Hi,
>
[quoted text clipped - 61 lines]
> > Thank you
> > Karl

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.