Initializing without assigning

C

Calle Pettersson

Coming from writing mostly in Java, I have trouble understanding how to
declare a member without initializing it, and do that later... In Java,
I would write something like
public static void main(String[] args) {
MyType aMember;
...
aMember = new MyType(...)
...
}
However, in C++ this does not seem to work. I declare in class (it's
for serial port communication) like this:

#include <fstream>
#include <boost/thread/thread.hpp>

class COMConn {
public:
COMConn(int port);
~COMConn();

char *send(char);
char *send(char, char[]);
private:
void connect(int);

std::ifstream input;
std::eek:fstream output;

boost::thread inputthread;
boost::thread outputthread;
};

But when I want to initialize the threads and streams, I want to do
something like

void COMConn::connect(int port) {
this->input = std::ifstream("COM1:");
this->inputthread = boost::thread(&input.read); // Not correct, needs
arguments, but skip that for now
//etc for the others
}

How do you go about doing this in C++? I found that I could do
this->input.open("COM1:");
But that is hardly something that will work in general. I find this
very confusing I must say... Thanks you for any advice
 
M

Michiel.Salters

Calle said:
Coming from writing mostly in Java, I have trouble understanding how to
declare a member without initializing it, and do that later...

General rule: don't. Just move the declaration to the point where you
initialize it, as any object is unusable until initialized.

You don't have to declare variables first; that's a old C habit which
is not
even needed in today's C, let alone in newer languages.

HTH
Michiel Salters
 
C

Calle Pettersson

General rule: don't. Just move the declaration to the point where you
initialize it, as any object is unusable until initialized.

You don't have to declare variables first; that's a old C habit which
is not
even needed in today's C, let alone in newer languages.

HTH
Michiel Salters
Really? But if I need this to be accessible to the class, but can't
call the constructor in the class definition? (Need to know which com
port in this case)
Also, I didn't know that variables could be added to a class if they
aren't specified in the header?
Or am I misunderstanding you?
 
V

Victor Bazarov

Calle said:
Really? But if I need this to be accessible to the class, but can't
call the constructor in the class definition? (Need to know which com
port in this case)
Also, I didn't know that variables could be added to a class if they
aren't specified in the header?
Or am I misunderstanding you?

It seems you're misunderstanding each other.

*Members* are declared first, in the class definition. Then, in the
initialiser list of a constructor, they are initialised. *Variables*
(non-member, stand-alone) don't need to be declared. Declare/define/
initialise them when needed.

class Class {
void *member; // a member: declaration
public:
Class() : member(0) {} // initialisation
};

int main() {
void *nonmember(0); // a variable: declaration/definition/init'n
}


V
 
T

Thomas J. Gritzan

Calle said:
#include <fstream>
#include <boost/thread/thread.hpp>

class COMConn {
public:
COMConn(int port);
~COMConn();

char *send(char);
char *send(char, char[]);
private:
void connect(int);

std::ifstream input;
std::eek:fstream output;

boost::thread inputthread;
boost::thread outputthread;
};

But when I want to initialize the threads and streams, I want to do
something like

void COMConn::connect(int port) {
this->input = std::ifstream("COM1:");
this->inputthread = boost::thread(&input.read); // Not correct, needs
arguments, but skip that for now
//etc for the others
}

Use initialization lists in the constructor if its possible:

void COMConn::connect(int port) : input("COM1:"), inputthread(&input.read)
{

}

This way the constructor of the member objects is called.
 
R

Richard Herring

Really? But if I need this to be accessible to the class, but can't
call the constructor in the class definition? (Need to know which com
port in this case)
Also, I didn't know that variables could be added to a class if they
aren't specified in the header?
Or am I misunderstanding you?
You're misunderstanding each other.

MS is saying that you don't need to declare local variables in a
function before you need them.

You're asking how to declare member variables of a class without
initialising them. The over-simplified answer is that you can't, because
the class's constructor must invoke _some_ constructor for each member.
If they need information that's not available at construction, you have
to pass it in by calling a "post-constructor" member function at some
later stage.

What you are probably overlooking is that C++, unlike Java, has value
semantics for class types. All the class members are actual value
objects, whereas the "members" of a Java class are really just disguised
pointers.

This means that the members of a C++ class "exist" in the sense that you
can pass around pointers or references to them as soon as they have been
constructed, and before any post-construction operations have taken
place.
 
M

mlimber

Thomas said:
Calle said:
#include <fstream>
#include <boost/thread/thread.hpp>

class COMConn {
public:
COMConn(int port);
~COMConn();

char *send(char);
char *send(char, char[]);
private:
void connect(int);

std::ifstream input;
std::eek:fstream output;

boost::thread inputthread;
boost::thread outputthread;
};

But when I want to initialize the threads and streams, I want to do
something like

void COMConn::connect(int port) {
this->input = std::ifstream("COM1:");
this->inputthread = boost::thread(&input.read); // Not correct, needs
arguments, but skip that for now
//etc for the others
}

Use initialization lists in the constructor if its possible:

void COMConn::connect(int port) : input("COM1:"), inputthread(&input.read)
{

}

This way the constructor of the member objects is called.

As TJG says, you should initialize every member somehow in the
constructor. It looks from your example, however, that you want to
delay creation of several objects until the connect function is called.
To do this, make the members pointers (since you're already using
Boost, use boost::scoped_ptr to get automatic cleanup), initialize them
to 0 in the constructor (scoped_ptr will do this automatically, also),
and then create the objects on-the-fly in your member function:

class COMConn
{
public:
// ...

private:
void connect(int);
std::ifstream input;
boost::scoped_ptr<boost::thread> inputthread;
};

void COMConn::connect( int port )
{
input.open( "COM1:");
inputthread.reset( new boost::thread( /*whatever*/ ) );
}

Cheers! --M
 
P

Pete Becker

Calle said:
Coming from writing mostly in Java, I have trouble understanding how to
declare a member without initializing it, and do that later... In Java,
I would write something like
public static void main(String[] args) {
MyType aMember;
...
aMember = new MyType(...)
...
}

The roughly equivalent C++ code would be

MyType *aMember = 0;
....
aMember = new MyType(...);
 
V

Victor Bazarov

Pete said:
Calle said:
Coming from writing mostly in Java, I have trouble understanding how
to declare a member without initializing it, and do that later... In
Java, I would write something like
public static void main(String[] args) {
MyType aMember;
...
aMember = new MyType(...)
...
}

The roughly equivalent C++ code would be

MyType *aMember = 0;
...
aMember = new MyType(...);

To the OP:

If 'aMember' is a local variable of the 'main' function ("method"), then
its name is wrong. It's not really a member, is it?

V
 
F

Frederick Gotham

Calle Pettersson posted:
Coming from writing mostly in Java, I have trouble understanding how to
declare a member without initializing it, and do that later...


If you want to use a pointer:

int main()
{
MyClass *p;

/* "p" contains garbage right now */


...


p = new MyClass; /* Object created here */


...

delete p; /* Object destroyed here */
}


Or perhaps if you wish to pre-allocate the memory:

#include <string>
#include <new>
using std::string;

union Aligned {
void *p;
long a;
long double b;
};


int main()
{
Aligned buf[
sizeof(string) / sizeof(Aligned)
+ !!(sizeof(string) % sizeof(Aligned)) ];


/* Suff happens... */

string &obj = *::new(&buf) string;


obj.~string();
}
 
V

Victor Bazarov

Frederick said:
Calle Pettersson posted:



If you want to use a pointer:

int main()
{
MyClass *p;

/* "p" contains garbage right now */


...


p = new MyClass; /* Object created here */

There is simply *no sence* to declare 'p' before this statement.
That Calle has already been told. Just declare/define/initialise
it right where you first use it.
...

delete p; /* Object destroyed here */
}


Or perhaps if you wish to pre-allocate the memory:

#include <string>
#include <new>
using std::string;

union Aligned {
void *p;
long a;
long double b;
};


int main()
{
Aligned buf[
sizeof(string) / sizeof(Aligned)
+ !!(sizeof(string) % sizeof(Aligned)) ];

Could you elaborate what this all is for? What are you trying
to accomplish with all that?
/* Suff happens... */

string &obj = *::new(&buf) string;


obj.~string();
}

V
 
R

Roland Pibinger

Pete said:
Calle said:
Coming from writing mostly in Java, I have trouble understanding how
to declare a member without initializing it, and do that later... In
Java, I would write something like
public static void main(String[] args) {
MyType aMember;
...
aMember = new MyType(...)
...
}
The roughly equivalent C++ code would be

MyType *aMember = 0;
...
aMember = new MyType(...);

To the OP:

If 'aMember' is a local variable of the 'main' function ("method"), then
its name is wrong. It's not really a member, is it?

Moreover, uninitialized variables are nor allowed in Java.
 
C

Calle Pettersson

Roland said:
Pete said:
Calle Pettersson wrote:
Coming from writing mostly in Java, I have trouble understanding how
to declare a member without initializing it, and do that later... In
Java, I would write something like
public static void main(String[] args) {
MyType aMember;
...
aMember = new MyType(...)
...
}
The roughly equivalent C++ code would be

MyType *aMember = 0;
...
aMember = new MyType(...);

To the OP:

If 'aMember' is a local variable of the 'main' function ("method"), then
its name is wrong. It's not really a member, is it?

Correct, I came up with that method just to show my point, and did so
in an erronus way, which understandably caused some confusion... I'll
try to be more careful in reading my code before posting in the future.
Moreover, uninitialized variables are nor allowed in Java.

Yes they are, as long as you don't try to access them. This will work
just fine:
class MyClass {
public static void main(String[] args) {
MyObject obj;
// blahblahblah
obj = new MyObject();
// etc
}
}
 
F

Frederick Gotham

Victor Bazarov posted:

Aligned buf[
sizeof(string) / sizeof(Aligned)
+ !!(sizeof(string) % sizeof(Aligned)) ];

Could you elaborate what this all is for? What are you trying
to accomplish with all that?


You can't do the following, because the array might not be aligned properly
to accommodate a particular type:

char buffer[sizeof(T)];

::new(buffer) T;


A possible solution would be to use a union to ensure correct alignment.

(Have you not seen this "trick" before? It's quite common.)
 
V

Victor Bazarov

Frederick said:
Victor Bazarov posted:

Aligned buf[
sizeof(string) / sizeof(Aligned)
+ !!(sizeof(string) % sizeof(Aligned)) ];

Could you elaborate what this all is for? What are you trying
to accomplish with all that?


You can't do the following, because the array might not be aligned
properly to accommodate a particular type:

char buffer[sizeof(T)];

::new(buffer) T;


A possible solution would be to use a union to ensure correct
alignment.

And how using the union you used with a 'void*', a 'long', and
a 'long double' going to accomplish "correct alignment"?
(Have you not seen this "trick" before? It's quite common.)

No, I haven't. Not to say that I've seen all the code in the world,
of course. It does strike me as strange that instead of simply
declaring a local object of type 'std::string' you declare an array
of some weird union type and then use placement new to allocate
a string. And you're saying that it's *quite common*? Indeed...

V
 
F

Frederick Gotham

Victor Bazarov posted:
And how using the union you used with a 'void*', a 'long', and
a 'long double' going to accomplish "correct alignment"?


Pick the largest integer type.

Pick the largest floating point type.

Pick the largest pointer type.

Put them together and you've got some very strict alignment.

Is it guaranteed to be strict enough? I'm not sure.

Is it likely to be strict enough? Yes, very.

No, I haven't. Not to say that I've seen all the code in the world,
of course. It does strike me as strange that instead of simply
declaring a local object of type 'std::string' you declare an array
of some weird union type and then use placement new to allocate
a string. And you're saying that it's *quite common*? Indeed...


Imagine you want to have a local object in a function, but you want some
other function to initialise it:

#include <new>

void OtherFunc(void * const);

int main()
{
char buffer[sizeof(MyType)];
/* Correct alignment not guaranteed */

OtherFunc(buffer);
}

void OtherFunc(void * const pbuf)
{
/* Do some stuff */

::new(pbuf) MyType();
}
 
V

Victor Bazarov

Frederick said:
Victor Bazarov posted:



Pick the largest integer type.

Pick the largest floating point type.

Pick the largest pointer type.

What happens to the pointer-to-member type? Did you know their size
is not predetermined? And there is no way to tell which one is the
largest.
Put them together and you've got some very strict alignment.

Yes, but alignment is *always* implementation-defined. Shouldn't we
stop trying to fake implementation-defined behaviour with standard
methods?
Is it guaranteed to be strict enough? I'm not sure.
Exactly.

Is it likely to be strict enough? Yes, very.

Sorry, not good enough.
No, I haven't. Not to say that I've seen all the code in the world,
of course. It does strike me as strange that instead of simply
declaring a local object of type 'std::string' you declare an array
of some weird union type and then use placement new to allocate
a string. And you're saying that it's *quite common*? Indeed...


Imagine you want to have a local object in a function, but you want
some other function to initialise it:

#include <new>

void OtherFunc(void * const);

int main()
{
char buffer[sizeof(MyType)];
/* Correct alignment not guaranteed */

OtherFunc(buffer);
}

void OtherFunc(void * const pbuf)
{
/* Do some stuff */

::new(pbuf) MyType();
}

Don't see it as "common" either. What you ususally see done is

MyType myobject = OtherFunc();

It's not entirely "initialisation" but with optimizations the
compiler is allowed to do, it's *more likely* to be correct, and
therefore *more common*, AFAICT.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top