473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

use of ' this() ' keyword in ctor design

will the third ctor in this class call the first two or just the null ctor?

public class B {
public B() { ... }
public B(string str) {.....}
public B(Token tkn, string str, int int_var) : this() { ... }
}

can I add an argument to this(str) and do the following so that ctor #3 does
call ctor#2 ?

public class B {
public B() { ... }
public B(string str) {.....}
public B(Token tkn, string str, int int_var) : this(str) { ... }
}

thank you, -greg
Nov 16 '05 #1
3 3861

"hazz" <ha**@sonic.net> wrote in message
news:us**************@TK2MSFTNGP10.phx.gbl...
will the third ctor in this class call the first two or just the null ctor?
public class B {
public B() { ... }
public B(string str) {.....}
public B(Token tkn, string str, int int_var) : this() { ... }
}
It will just call the null constructor (public B())

can I add an argument to this(str) and do the following so that ctor #3 does call ctor#2 ?

public class B {
public B() { ... }
public B(string str) {.....}
public B(Token tkn, string str, int int_var) : this(str) { ... }
}

Yes, in this case, ctor #2 is executed first then ctor #3 is executed - ctor
#1 is not executed
thank you, -greg

Nov 16 '05 #2

"hazz" <ha**@sonic.net> wrote in message
news:us**************@TK2MSFTNGP10.phx.gbl...
will the third ctor in this class call the first two or just the null
ctor?

public class B {
public B() { ... }
public B(string str) {.....}
public B(Token tkn, string str, int int_var) : this() { ... }
this() calls only the constuctor with no arguments.
}

can I add an argument to this(str) and do the following so that ctor #3
does
call ctor#2 ?

public class B {
public B() { ... }
public B(string str) {.....}
public B(Token tkn, string str, int int_var) : this(str) { ... }
}


Yes, that's exactly how you do it.
Nov 16 '05 #3
hazz wrote:
will the third ctor in this class call the first two or just the null ctor?
It will call the one that matches the parameters you supplied, so the
zero param constructor.
public class B {
public B() { ... }
public B(string str) {.....}
public B(Token tkn, string str, int int_var) : this() { ... }
}

can I add an argument to this(str) and do the following so that ctor #3 does
call ctor#2 ?
Yes
public class B {
public B() { ... }
public B(string str) {.....}
public B(Token tkn, string str, int int_var) : this(str) { ... }
}

thank you, -greg

JB
Nov 16 '05 #4

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

Similar topics

6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
4
by: MJ | last post by:
Hi can I define constructor as protected?? if yes than what are the implications and what is the diff bet defining the ctor as protected and public Mayur
3
by: hazz | last post by:
I know it violates alot of design rules but is it possible to have an ADO.NET database query in a constructor? As cheesy as that sounds, it may simplify an otherwise very complex design in a...
5
by: ChrisB | last post by:
Hello: An object that is a field in another object has a constructor that requires a reference to the containing object: // object fields ChildObject childObject = new ChildObject(this); ...
8
by: Grizlyk | last post by:
Good morning. Look here: http://groups.google.com/group/fido7.ru.cpp.chainik/browse_frm/thread/7341aba5238c0f79 and here:...
5
by: peifeng_w | last post by:
Hi, try the following code with flag=0/1/2. #include<iostream> using namespace std; #define flag 2//option:0,1,2 class C {
83
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
8
by: 2b|!2b==? | last post by:
typedef struct llist_entry_s llist_entry; /* opaque type */ struct llist_entry_s { llist_entry * next; char * keyword; char * value; llist_entry() :next(0), keyword(0), value(0) {
2
by: subramanian100in | last post by:
If we do not provide any ctor for a class, the compiler provides the default ctor and copy ctor if needed. Consider a class Test. Suppose we provide some ctor in class Test but do not provide...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.