Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
69 user(s) are online (40 user(s) are browsing Forums)

Members: 2
Guests: 67

kas1e, walkero, more...

Headlines

 
  Register To Post  

How to initialize a struct in a class?
Quite a regular
Quite a regular


See User information
This is a generic C++ question.

Considers this:

struct A {
int x;
float y;
and a laaarge amount of more memobers that I really don't want to initialize manually, or by manually using memset.
};

class B {
public:
B::B();
int foo;
A bar;
}

B::B() :
foo(0), // <-normal contructor initialization of a member.
bar() // <---- what would this do? Anything at all?
{}


Software developer for Amiga OS3 and OS4.
Develops for OnyxSoft and the Amiga using E and C and occasionally C++
Go to top
Re: How to initialize a struct in a class?
Just popping in
Just popping in


See User information
in c++ structs can have constructors

Go to top
Re: How to initialize a struct in a class?
Just can't stay away
Just can't stay away


See User information
I think a general rule is that you should never use "struct" keyword when writing C++ code ("class" should be used instead) as it only causes confusion and doesn't really serve any purpose.

As implied to by Shadow above pretty much the only difference between "struct" and "class" keywords in C++ is that if you use "struct" the access type of fields and methods defaults to "public" while with "class" it defaults to "private" (IIRC).

Go to top
Re: How to initialize a struct in a class?
Quite a regular
Quite a regular


See User information
Well the struct is from a public include (I think) and is going to be used by C-code. It only happen to be used from within a C++ class and Coverity complained that it wasn't initialized.

Software developer for Amiga OS3 and OS4.
Develops for OnyxSoft and the Amiga using E and C and occasionally C++
Go to top
Re: How to initialize a struct in a class?
Just can't stay away
Just can't stay away


See User information
If you compile with -std=c++0x or -std=gnu++0x you can do it like this:

B::B() :
foo(0),
bar({0})
{}

Go to top
Re: How to initialize a struct in a class?
Quite a regular
Quite a regular


See User information
Quote:

salass00 wrote:
If you compile with -std=c++0x or -std=gnu++0x you can do it like this:

B::B() :
foo(0),
bar({0})
{}


Yeah, that would be the normal way of initializing a struct, using {}. Interesting. I wonder if Visual Studio and RVCT for ARM will swallow this code... :)

Software developer for Amiga OS3 and OS4.
Develops for OnyxSoft and the Amiga using E and C and occasionally C++
Go to top
Re: How to initialize a struct in a class?
Just popping in
Just popping in


See User information
If you've got structure A inside class B and want to initialize the structure A while constructing object of class B just call desired constructor of structure A within constructor's initialisation list.
Example:
struct A
{
   
// a set of members
   
int m_xm_ym_z;
   
// a set of constructors
   
A() : m_x(1), m_y(2), m_y(3) {}
   
A(int xint yint z) : m_x(x), m_y(y), m_z(z) {}
};

class 
B
{
   
A m_a;
   
// here we call constructor of class A in initialisation list
   
B() : A(456) {}
};

This should answer your question.

Go to top
Re: How to initialize a struct in a class?
Quite a regular
Quite a regular


See User information
@RNS

Thanks, but I can't change the struct, it's public and fixed and needs to work in a C environment.

Software developer for Amiga OS3 and OS4.
Develops for OnyxSoft and the Amiga using E and C and occasionally C++
Go to top
Re: How to initialize a struct in a class?
Just can't stay away
Just can't stay away


See User information
Quote:

Thanks, but I can't change the struct, it's public and fixed and needs to work in a C environment.


You should be able to subclass it though and add a constructor that way (I think).

Go to top
Re: How to initialize a struct in a class?
Quite a regular
Quite a regular


See User information
@salass00

Quote:

You should be able to subclass it though and add a constructor that way (I think).


Sure, but none of it is really necessary, I just want Coverity to shut up the simplest possible way

Software developer for Amiga OS3 and OS4.
Develops for OnyxSoft and the Amiga using E and C and occasionally C++
Go to top
Re: How to initialize a struct in a class?
Just popping in
Just popping in


See User information
Hi

If you're calling it from c code, you can have a stub routine written in C++ that has extern "C" around its declaration. The rest of your c code can then call this stub routine and all will happily co-exist.

Go to top
Re: How to initialize a struct in a class?
Quite a regular
Quite a regular


See User information
@billyfish

So far all solutions, except ({0}) which didn't compile on Visual Studio nor RVCT for ARM, has been much more complicated than a simple memset().

Remember, I want to get Coverity (which is a software quality check tool) quiet. It simply complains about this member not being initialized in the constructor, which code-wize is irrelevant because once it is used it is properly filled in completely.

To enlighten the situation: The struct in question is struct jpeg_decompress_struct from jpeglib which is used from within a C++ class. The struct isn't filled in until it is used in a save method and Coverity has a problem with that. It wants all members of the class to be initialized in the constructor. Period.

Software developer for Amiga OS3 and OS4.
Develops for OnyxSoft and the Amiga using E and C and occasionally C++
Go to top
Re: How to initialize a struct in a class?
Just can't stay away
Just can't stay away


See User information
Quote:

To enlighten the situation: The struct in question is struct jpeg_decompress_struct from jpeglib which is used from within a C++ class. The struct isn't filled in until it is used in a save method and Coverity has a problem with that. It wants all members of the class to be initialized in the constructor. Period.


If you use it in one method only and you fill it in there before using it wouldn't it make more sense to just declare it as a local variable in that method?

Go to top
Re: How to initialize a struct in a class?
Just popping in
Just popping in


See User information
I'm not sure if its what you're after but what I was suggesting was that the struct that you need in your c code is in a c++ file, but wrapped in a method that is declared as extern "c" i.e

--- foo.hpp ---

extern "C"
{
struct Foo *GetTheCPlusPlusStructure ();
}

--- foo.cpp ---

struct Foo *GetTheCPlusPlusStructure () can use any c++ code e.g. for construction, member initialisation, etc.

so foo.cpp is comppiled as c++ code.

The rest of your c code can be compiled as normal as c code and just include foo.hpp and link to foo.o.

Does that make sense and/or help?

Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2023 The XOOPS Project