473,469 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

reg. constructor parameters

Hi all,

I have these classes (for a GUI library) that uses
alot of variables that need to be set at c-tor time

i.e.

class Button : Widget {
public:
Button (int x, int y) : Widget (BUTTON),
m_x (x),
m_y (y),
name (""),
etc /.../

Is it true that this generates better (meaning faster)
code than doing this?

Button (int x, int y) {
this->m_x = x;
this->m_y = y;
etc /.../

Thanks.

-- Pelle
Jul 23 '05 #1
3 1140
Pelle Beckman wrote:
I have these classes (for a GUI library) that uses
alot of variables that need to be set at c-tor time

i.e.

class Button : Widget {
public:
Button (int x, int y) : Widget (BUTTON),
m_x (x),
m_y (y),
name (""),
etc /.../

Is it true that this generates better (meaning faster)
code than doing this?

Button (int x, int y) {
this->m_x = x;
this->m_y = y;
etc /.../


Generally speaking, yes. Please refer to FAQ. Search for "assignment
initialization".

V
Jul 23 '05 #2
Pelle Beckman wrote:
Hi all,

I have these classes (for a GUI library) that uses
alot of variables that need to be set at c-tor time

i.e.

class Button : Widget {
public:
Button (int x, int y) : Widget (BUTTON),
m_x (x),
m_y (y),
name (""),
etc /.../

Is it true that this generates better (meaning faster)
code than doing this?

Button (int x, int y) {
this->m_x = x;
this->m_y = y;
etc /.../

Thanks.

-- Pelle


Yes almost always but there is just 1 exception to the rule. Pls go
through
http://www.parashift.com/c++-faq-lit....html#faq-10.6

Jul 23 '05 #3
> Yes almost always but there is just 1 exception to the rule. Pls go
through
http://www.parashift.com/c++-faq-lit....html#faq-10.6


Hi, is there any effect in applying this when having virtual
inheritance.

Jul 23 '05 #4

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

Similar topics

18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
3
by: anddos | last post by:
ive got this sample from a book i am learning from c++ , and need abit of help on constructor #ifndef CAR_HPP #define CAR_HPP class Car { public: Car(); Car(int initRadioFreq, int...
19
by: Daniel Billingsley | last post by:
I think it's really nice to be able to do code like someVariable = someMethod(new SomeClass("some text")); However, as method signatures are number and types of parameters, you're limited to...
26
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
8
by: Sam Kuehn | last post by:
How do I accomplish the fallowing (is it even possible). Say I write a UserControl "MyControl.ascx". Now I use LoadControl("MyControl.ascx"). But I really want MyControl to require parameters in...
3
by: denis_browne | last post by:
When writing the following code: class Base { Base(const Base &rhs) {} ~Base(); }; void f() { Base b;
7
by: Geoffrey | last post by:
Hello, I wan't to have optionnal parameter in my cronstructor, so, I define 2 constructors. when I call the constructor without the "optional" parameter, I want to call the constructor...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
13
by: sam_cit | last post by:
Hi Everyone, I have the following unit to explain the problem that i have, class sample { public : sample() { printf("in sample...\n"); }
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
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,...
0
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 ...

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.