473,802 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange problem about constructor

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::myclas s(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
Jul 22 '05 #1
13 1434
"John" <jo*********@ya hoo.com> wrote in message
news:c3******** *************** ***@posting.goo gle.com...
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?
Jul 22 '05 #2
"Andrew Koenig" <ar*@acm.org> wrote...
"John" <jo*********@ya hoo.com> wrote in message
news:c3******** *************** ***@posting.goo gle.com...
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?


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
Jul 22 '05 #3

"John" <jo*********@ya hoo.com> wrote in message
news:c3******** *************** ***@posting.goo gle.com...
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::myclas s(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
Jul 22 '05 #4
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::en dl;
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

"Andrew Koenig" <ar*@acm.org> wrote in message news:<sE******* **************@ bgtnsc05-news.ops.worldn et.att.net>...
"John" <jo*********@ya hoo.com> wrote in message
news:c3******** *************** ***@posting.goo gle.com...
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?

Jul 22 '05 #5
"John" <jo*********@ya hoo.com> wrote in message
news:c3******** *************** **@posting.goog le.com
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::en dl;
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;
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #6
/*
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;
}
Jul 22 '05 #7
grahamo wrote:


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.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #8
> 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::e ndl;
}

In this example we have 1 2 in output. And so your example does not
show your problem, right?
Jul 22 '05 #9
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
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1831
by: grundmann | last post by:
Hello, i got a strange compiler error. When compiling the following: // forward declarations typedef AvlTree<LineSegment,LineSegmentComperator> LSTree; void handleEventPoint (const EventPoint& , LSTree& , double&, std::list<IntersectionPoint>& );
4
1299
by: alkee.na | last post by:
Hey, Here's a sample code you can easily guess the result. <result 1> #include <iostream> using namespace std; class AA { public: AA() { cout<<"AA();"<<endl; }
29
5082
by: Charles Law | last post by:
Further to my issue about user controls, I have a problem with DesignMode. Here is the project hierarchy: MainApp |_ Project1 |_ SubProject (UserControl) SubProject has a default constructor (New) which does the MyBase.New() thing and then InitializeComponent().
3
1406
by: rdh | last post by:
I'm using List<> to store a class of positions. Position are basically latitude and longitudes (doubles). the line is something like List<position> P = new List<position>(); Then I start adding to the List. P.add(Positions);
3
2432
by: senfo | last post by:
I developed a Windows control in VS 2005 that inherits from the PictureBox Control that adds the ability to select images in a Windows application. It is, however, experiencing a strange issue that I can't explain. One of the desired affects was to provide the ability to change the background color on images that were selected, as well as setting the background color back to its original color after the image had been deselected. To...
28
2147
by: charlie | last post by:
Hi, I found an article on informit.com that talks about C++ casts http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=285&rl=1 The strange thing is the author says the following code will *not* compile: double d=15.95; int n= static_cast<int(d);
12
2274
by: StephQ | last post by:
I have a class Bounds with two constructors: class Bounds { private: list<SegmentupperLinearSpline; // Upper bound. list<SegmentlowerLinearSpline; // Lower bound. ....
6
3345
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
Yesterday Visual Studio gave me a strange error both at compiletime and at designtime that had no obvious connection to anything I had changed recently. After some effort tracking down the problem I discovered first a workaround, then the real cause of the problem. I would like to understand why what I am doing is frowned upon by Visual Studio and how to do this properly. My application is in one solution; supporting libraries including...
12
5654
Dormilich
by: Dormilich | last post by:
Hi, I’ve encountered quite a strange error when loading one of my scripts in Safari (4.0.3/Mac): I have no idea, what Safari is complaining about, since it works in FF 3.5 and Opera 10, does anyone of you have an idea? thanks, Dormi // by Gavin Kistner
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.