LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
 
Search this Thread
Old 05-25-2012, 08:46 AM   #1
Netooo
LQ Newbie
 
Registered: Jun 2010
Location: Guadalajara, Jalisco, Mexico
Distribution: Slackware
Posts: 20

Rep: Reputation: 0
Question C++, constructor overloading


If I have more than one constructor in a class, how do I know wich of them is the default one?
 
Old 05-25-2012, 08:50 AM   #2
dugan
Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 5,476

Rep: Reputation: 1708Reputation: 1708Reputation: 1708Reputation: 1708Reputation: 1708Reputation: 1708Reputation: 1708Reputation: 1708Reputation: 1708Reputation: 1708Reputation: 1708
Aren't "the constructor that doesn't take any parameters" and "the default constructor" synonymous?
 
1 members found this post helpful.
Old 05-25-2012, 08:51 AM   #3
Netooo
LQ Newbie
 
Registered: Jun 2010
Location: Guadalajara, Jalisco, Mexico
Distribution: Slackware
Posts: 20

Original Poster
Rep: Reputation: 0
Lightbulb

Quote:
Originally Posted by dugan View Post
Aren't "the constructor that doesn't take any parameters" and "the default constructor" synonymous?
I don't know, are they? =/
 
Old 05-25-2012, 09:31 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Yes, dugan is correct. If you supply any constructor with arguments, the compiler will not supply a default constructor. If you need one, you'll have to provide it. For example, consider the following simple class:

Code:
class Foo
{
  public:
    Foo(int val):x(val) {}

  private:
    int x;
};
The following statement will not compile, as there is no default constructor:

Code:
Foo f;
In this case, you need to add explicitly a no argument constructor to your class definition.

Last edited by Nylex; 05-25-2012 at 09:36 AM.
 
Old 05-26-2012, 08:34 AM   #5
Netooo
LQ Newbie
 
Registered: Jun 2010
Location: Guadalajara, Jalisco, Mexico
Distribution: Slackware
Posts: 20

Original Poster
Rep: Reputation: 0
Thumbs up

Thanks guys, that's what I needed to know.
 
  


Reply

Tags
c++, constructor


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Constructor takes string => constructor not called...? (C++) JohnGraham Programming 4 02-16-2012 12:37 PM
C++ constructor/copy constructor issues in g++ 3.4.6 abhinav.dube15 Programming 7 03-13-2011 08:27 AM
Compilation problem overloading operator () to template class constructor. ianix Programming 1 04-19-2006 01:16 PM


All times are GMT -5. The time now is 08:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration