reg. constructor parameters

P

Pelle Beckman

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
 
V

Victor Bazarov

Pelle said:
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
 
J

Jaspreet

Pelle said:
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-lite/ctors.html#faq-10.6
 

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
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top