Strange problem about constructor

J

John

Hi all:

In my code I define a class with inline constructor. But it does not
work. I describe the class as below:

myclass{
public:
myclass(int a, int b) { r1 = a; r2 = b;}

protected:
int r1;
int r2;
double z;
}

But the constructor does not work. When I declare an object of the
class, the constructor can not correctly initialize the variables.

Then I modify the code as below:

myclass{
public:
myclass(int a, int b);

protected:
int r1;
int r2;
double z;
}

myclass::myclass(int a, int b) {
r1 = a;
r2 = b;
}

It works. When I declare an object of the class, the constructor can
correctly initialize variables.

What is the problem?

Thanks a lot.

John
 
A

Andrew Koenig

In my code I define a class with inline constructor. But it does not
work. I describe the class as below:

myclass{
public:
myclass(int a, int b) { r1 = a; r2 = b;}

protected:
int r1;
int r2;
double z;
}

But the constructor does not work. When I declare an object of the
class, the constructor can not correctly initialize the variables.

How do you know the program isn't working? The code you show above is
obviously not the actual code, because it won't compile. So what did you
actually do, and how do you know that it's not behaving correctly?
 
V

Victor Bazarov

Andrew Koenig said:
How do you know the program isn't working? The code you show above is
obviously not the actual code, because it won't compile. So what did you
actually do, and how do you know that it's not behaving correctly?

Absolutely agree with Andrew here. Just a side note, I've seen many
times that a newbie would write

myclass(int a, int b) { int r1 = a; int r2 = b; }

without even realising that it's different from the intended way. It
can even happen that while typing in their problem into a newsgroup
posting they inadvertently correct the error. Of course, it would be
nice if compilers warned about situations like that...

To the OP: do NOT use assignment, use the initialiser list.

Victor
 
J

John Harrison

John said:
Hi all:

In my code I define a class with inline constructor. But it does not
work. I describe the class as below:

myclass{
public:
myclass(int a, int b) { r1 = a; r2 = b;}

protected:
int r1;
int r2;
double z;
}

But the constructor does not work. When I declare an object of the
class, the constructor can not correctly initialize the variables.

Then I modify the code as below:

myclass{
public:
myclass(int a, int b);

protected:
int r1;
int r2;
double z;
}

myclass::myclass(int a, int b) {
r1 = a;
r2 = b;
}

It works. When I declare an object of the class, the constructor can
correctly initialize variables.

What is the problem?

Without seeing an entire program it is impossible to say. Both code examples
you posted are equally incorrect (missing semi colons, missing keywords etc)
but as far as writing an inline constructor goes you did that correctly. So
whatever your problem was it was somewhere in the code you didn't post. This
is a very common situation with newbies posting to this group, they don't
understand where they are going wrong and often post the part of the program
that is correct, not the part that is wrong. If you want help then post the
real code, and post a complete program.

john
 
J

John

Thanks for reply.
The code is not the original code. I wish I could post the original
code here. But I can not, because the code is large and complex. The
class in my post shows the structure of the class that generates
problem. In my original code, I output the result after an object is
declared, like:

myclass v(2,3);
std::cout<<v.a<<" "<<v.b<<std::endl;
The result is not 2 and 3, but some large numbers.

But when I do not use inline constructor, the problem does not happen.
I know it is difficult to figure out the problem without the original
code post here.

Thanks anyway.

John
 
J

John Carson

John said:
Thanks for reply.
The code is not the original code. I wish I could post the original
code here. But I can not, because the code is large and complex. The
class in my post shows the structure of the class that generates
problem.

If the code does not generate the problem, then it does NOT show the
structure of the class that generates the problem. Produce a code sample
that actually shows the problem and I am sure we can quickly solve it.
In my original code, I output the result after an object is
declared, like:

myclass v(2,3);
std::cout<<v.a<<" "<<v.b<<std::endl;
The result is not 2 and 3, but some large numbers.

a and b are not members of myclass as you specified it (the members are r1
and r2). Further, those members are protected, so you cannot access them
from, say, main().

The following works.

#include <iostream>
class myclass{
public:
myclass(int a, int b) { r1 = a; r2 = b;}
void print()
{
std::cout<< r1 <<" "<< r2 <<std::endl;
}
protected:
int r1;
int r2;
double z;
};

int main()
{
myclass v(2,3);
v.print();
return 0;
}
 
G

grahamo

/*
This is a long shot but are you by any chance using a flakey compiler?
i.e. not Suns Forte C++, GNU c++, VC++ or any of the other big names.
If the compiler isn't very well known or is in beta release there's a
SLIM SLIM SLIM possibility that they've got the inlining bit wrong or
something. Just for kicks post your compilers version of CC
-V(ersion). Methinks you're using a well dodgy compiler or have an
error somewhere else in your code/app.

In any case, give this code a lash. If it prints out anything other
than

entered main
myclass.r1: 2 myclass.r2: 3

then we've narrowed down your problem a bit. If it does print out the
above text as above, then you have to look elsewhere as the inlined
constructor is not an issue.


BTW somebody mentioned that you should use the initialiser list in
your ctor. I think that applies only for types other than built in
types no? i.e. there's no overhead in using assignment with integers
etc. No?




*/

#include <iostream>


class myclass
{
public:

// if you get the expected output with this code, recompile without
the
// inline keyword and see what you get.
inline myclass(int a, int b)
{
r1 = a;
r2 = b;
}

// protected: you won't be able to get your hands on these members
externally.
// so I'll leave them in the public interface

int r1;
int r2;
double z;

};

int main(int argc, char** argv)
{
std::cout << "entered main" << std::endl;
myclass mc(2,3);
std::cout << "myclass.r1: " << mc.r1 << " myclass.r2: " << mc.r2 <<
std::endl;
return 0;
}
 
K

Karl Heinz Buchegger

grahamo said:
BTW somebody mentioned that you should use the initialiser list in
your ctor. I think that applies only for types other than built in
types no? i.e. there's no overhead in using assignment with integers
etc. No?

There is no overhead. But even with builtin types it is easy to
show an example where initializer lists work, while assignment doesn't.
Hint: Think of const members. Think of references.

So getting into the habit of using initializer lists doesn't do any
harm, but is vital in some cases.
 
N

New_user

Thanks for reply.
The code is not the original code. I wish I could post the original
code here. But I can not, because the code is large and complex.

Ok, try to minimize and simplify your code so, that it is "simple" and
still buggy. It seems to me, that you should give us more detailed
description of the problem?

For example, compile this:

#include <iostream>

class A
{
public:
A(int a, int b)
{
a_ = a;
b_ = b;
}
int a_;
int b_;
};

int main()
{
A a(1,2);
std::cout<<a.a_<<" "<<a.b_<<std::endl;
}

In this example we have 1 2 in output. And so your example does not
show your problem, right?
 
J

JKop

Victor Bazarov posted:
To the OP: do NOT use assignment, use the initialiser list.


Just to expand on that a little...

Consider the following:


class BankAccountData
{
public:
unsigned long int const ID;

BankAccountData(unsigned long int& const in_ID) : ID(in_ID)
{
;
}
};


Without the initializer list, you couldn't work with ID, as it is const.


-JKop
 
J

John

Hi all:

Thanks a lot. I post a simplified version of my code.

---------------
header-file.h
---------------
#include <list>
#include <algorithm>

myclass{
friend class yclass;
public:
myclass(u_int32_t a, u_int32_t b) { r1 = a; r2 = b;}
protected: //u_int32_t is defined type, a kind of integer.
u_int32_t r1;
u_int32_t r2;
double z;
}

yclass{

public:

.....
void m_insert(u_int32_t id, u_int32_t bd);
bool m_lookup(u_int32_t id, u_int32_t bd);
void m_purge(void);
void funct(u_int32_t id, u_int32_t bd);

std::list< myclass > myclass_list;

.....
}


-----------------------------------
file.cc
-----------------------------------
#include <header-file.h>

void yclass::m_insert(u_int32_t d, u_int32_t bd) {
myclass b(d, bd);
std::cout<<"r1:"<<b.r1<<" r2:"<<b.r2<<std::endl; //LINE 1
b.z = TIME;//a constant.
myclass_list.push_back(b);

std::cout<<"insert--d:"<<d<<" bd:"<< bd<<std::endl;
std::list< myclass >::const_iterator pos;
for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos)
std::cout<<"pos->r1:"<<(*pos).r1<<" r2:"<<(*pos).r2<<"
z:"<<pos->z<<std::endl;

}

bool yclass::m_lookup(u_int32_t d, u_int32_t bd) {
std::list< myclass >::const_iterator pos;
std::cout<<"lookup--d:"<<d<<" bd:"<< bd<<std::endl;
for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos){
std::cout<<"(*pos).r1:"<<pos->r1<<" r2:"<< pos->r2<<std::endl;
if (((*pos).r1 == d) && ((*pos).r2 == bd)){
std::cout<<"find it"<<std::endl; //LINE 2
return true;
}
}
return false;
}

void yclass::m_purge() {
std::list< myclass >::iterator pos;
for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos){
if((*pos).z <= 10) {
myclass_list.erase(pos);
--pos;
}
}
}

void funct(u_int32_t id, u_int32_t bd){

.........
if(!m_lookup(id, bd)){
m_insert(id, bd); //LINE 3
}

.........

}

If LINE 3 is executed, e.g., m_insert(2,3), LINE 1 can not output 2
and 3, but two large numbers. So LINE 2 is never executed, since the
list -- myclass_list does not store the correct value. But if I do not
use inline constructor, the output is correct.

I post all the code related to myclass. I hope the bug has been
exposed.

Thanks again for the help.

John
 
J

Jeff Flinn

Forgive the top post, but again the code you provided below is not
compilable, even with cursory examination. ie: myclass{ ... } is not C++,
You need to prefix with the keyword 'class' and terminate the class
declaraton with ';'

Get this code to compile and show us the problem.

Jeff F
 
V

Victor Bazarov

John said:
Thanks a lot. I post a simplified version of my code.

Too "simplified" to be useful. Please read the FAQ 5.8.

This doesn't seem to be relevant at all.
myclass{
friend class yclass;
public:
myclass(u_int32_t a, u_int32_t b) { r1 = a; r2 = b;}
protected: //u_int32_t is defined type, a kind of integer.
u_int32_t r1;
u_int32_t r2;
double z;
}

yclass{

public:

.....
void m_insert(u_int32_t id, u_int32_t bd);
bool m_lookup(u_int32_t id, u_int32_t bd);
void m_purge(void);
void funct(u_int32_t id, u_int32_t bd);

std::list< myclass > myclass_list;

.....
}

What are the values of these arguments when you step into this
function?
myclass b(d, bd);

What do you see in the debugger when you step over this definition?
std::cout<<"r1:"<<b.r1<<" r2:"<<b.r2<<std::endl; //LINE 1
b.z = TIME;//a constant.
myclass_list.push_back(b);

std::cout<<"insert--d:"<<d<<" bd:"<< bd<<std::endl;
std::list< myclass >::const_iterator pos;
for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos)
std::cout<<"pos->r1:"<<(*pos).r1<<" r2:"<<(*pos).r2<<"
z:"<<pos->z<<std::endl;

}

bool yclass::m_lookup(u_int32_t d, u_int32_t bd) {

What are the values of the arguments here? Are they correct?
std::list< myclass >::const_iterator pos;
std::cout<<"lookup--d:"<<d<<" bd:"<< bd<<std::endl;

What output do you see here?
for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos){
std::cout<<"(*pos).r1:"<<pos->r1<<" r2:"<< pos->r2<<std::endl;
if (((*pos).r1 == d) && ((*pos).r2 == bd)){
std::cout<<"find it"<<std::endl; //LINE 2
return true;
}
}
return false;
}

void yclass::m_purge() {
std::list< myclass >::iterator pos;
for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos){
if((*pos).z <= 10) {
myclass_list.erase(pos);
--pos;
}
}
}

Seems that 'm_purge' is also errelevant.
void funct(u_int32_t id, u_int32_t bd)
{

What are the values of 'id' and 'bd' here?
.........
if(!m_lookup(id, bd)){
m_insert(id, bd); //LINE 3
}

.........

}

Who is calling this function? How is it called?
If LINE 3 is executed, e.g., m_insert(2,3), LINE 1 can not output 2
and 3, but two large numbers. So LINE 2 is never executed, since the
list -- myclass_list does not store the correct value. But if I do not
use inline constructor, the output is correct.

I post all the code related to myclass.

No, you didn't post _all_ code. You posted _some_ code. Non-compilable,
first. Incomplete, second.

I hope the bug has been
exposed.

How can it have been? So far only your inattentiveness to requirements
has.

V
 
J

John Harrison

John said:
Hi all:

Thanks a lot. I post a simplified version of my code.

---------------
header-file.h
---------------
#include <list>
#include <algorithm>

myclass{
friend class yclass;
public:
myclass(u_int32_t a, u_int32_t b) { r1 = a; r2 = b;}
protected: //u_int32_t is defined type, a kind of integer.
u_int32_t r1;
u_int32_t r2;
double z;
}

yclass{

public:

.....
void m_insert(u_int32_t id, u_int32_t bd);
bool m_lookup(u_int32_t id, u_int32_t bd);
void m_purge(void);
void funct(u_int32_t id, u_int32_t bd);

std::list< myclass > myclass_list;

.....
}


-----------------------------------
file.cc
-----------------------------------
#include <header-file.h>

void yclass::m_insert(u_int32_t d, u_int32_t bd) {
myclass b(d, bd);
std::cout<<"r1:"<<b.r1<<" r2:"<<b.r2<<std::endl; //LINE 1
b.z = TIME;//a constant.
myclass_list.push_back(b);

std::cout<<"insert--d:"<<d<<" bd:"<< bd<<std::endl;
std::list< myclass >::const_iterator pos;
for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos)
std::cout<<"pos->r1:"<<(*pos).r1<<" r2:"<<(*pos).r2<<"
z:"<<pos->z<<std::endl;

}

bool yclass::m_lookup(u_int32_t d, u_int32_t bd) {
std::list< myclass >::const_iterator pos;
std::cout<<"lookup--d:"<<d<<" bd:"<< bd<<std::endl;
for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos){
std::cout<<"(*pos).r1:"<<pos->r1<<" r2:"<< pos->r2<<std::endl;
if (((*pos).r1 == d) && ((*pos).r2 == bd)){
std::cout<<"find it"<<std::endl; //LINE 2
return true;
}
}
return false;
}

void yclass::m_purge() {
std::list< myclass >::iterator pos;
for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos){
if((*pos).z <= 10) {
myclass_list.erase(pos);
--pos;
}
}
}

void funct(u_int32_t id, u_int32_t bd){

.........
if(!m_lookup(id, bd)){
m_insert(id, bd); //LINE 3
}

.........

}

If LINE 3 is executed, e.g., m_insert(2,3), LINE 1 can not output 2
and 3, but two large numbers. So LINE 2 is never executed, since the
list -- myclass_list does not store the correct value. But if I do not
use inline constructor, the output is correct.

I post all the code related to myclass. I hope the bug has been
exposed.

Perhaps the problem is related to u_int32_t, how is that defined?

There is nothing wrong with your inline constructor.

john
 

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

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top