DM>You seem to be misinterpreting what I said. We're talking about
DM>how it looks to the new C++ programmer
TH> Maybe the discussion *should* be about how it looks to the developers
TH> of C++, namely Benjemin what's his name. [grin]
There can be two threads on this topic if you wish - one being the advanced
thread, and the other being the thread trying to get the newbie to understand
the advanced thread. :-) Wow - multithreading! :-)
TH> Yes, and it is important to get those concepts in correct
TH> *perspective* and accurately defined. There are certain rules no
TH> matter what level of programming one is at. And in C++ one of the
TH> rules is that there is only *one* _default_ constructor, no
TH> arguments, no returns. All other constructors are there to provide
TH> the desired functionality.
One default constructor per class.
class foo
{
foo(); // foo's default constructor
};
class bar
{
bar(float pressure=101.5); // bar's default constructor
};
[If you don't get bar, it's a conversion from kilopascals to... oh nevermind.
]
These do the same thing - allow you to create either a foo or a bar without
any parameters. But they look different and are thus confusing if you try to
combine them. Worse, the latter one is "two" functions in one - taking
either 0 or 1 parameter(s). The same code (with different parameters) is
called both for bar b1; and bar b2(100.0);. I can understand the difficulty
in understanding both of these at the same time, so I prefer the ability to
pull the concepts apart as long as we eventually bring them together.
[C++ programmers usually come from other languages]
TH> True, overloading is an uncommon but nifty feature. But not default
TH> arguments - which, BTW, are not the same thing as a default
TH> constructor. Default arguments (or parameters, more correctly) can
TH> exist in *any* function whether it's a method to a class or not. A
In C++, yes. In other languages... no. So we must deal with this concept...
:-)
TH> default constructor *must* exist in every class. If it is not
On the contrary - it does NOT need to exist in ANY class.
foo.h=====
class foo
{
public:
foo(int i);
int m_i;
private:
foo(); // default constructor?
};
foo.cc====
foo::foo(int i) : m_i(i) {}
//foo::foo() ... not defined - doesn't exist!
=====
Since the default constructor cannot be called, we don't need it.
TH> specified (ie. one that takes no arguments), then the compiler will
TH> create one - behind the programmers back.
Nope. :-) Only if there is no constructor there. If there IS _ANY_
constructor, the default will not be put there for you. For example... I
just wrote the following:
====foo.cc====
class foo
{
public:
foo(int i) : m_i(i) {}
int m_i;
};
void usefoo()
{
foo f; // line 10
}
============
Compiled it with:
============
[0] d:\tmp\src\g>gcc -v
gcc version 2.7.2.1
[0] d:\tmp\src\g>gcc -c foo.cc
foo.cc: In function `void usefoo()':
foo.cc:10: no matching function for call to `foo::foo ()'
foo.cc:6: candidates are: foo::foo(const foo &)
foo.cc:4: foo::foo(int)
foo.cc:10: in base initialization for class `foo'
foo.cc:10: warning: unused variable `class foo f'
============
See the candidates? The copy constructor is built (line 6???), but no
default constructor.
TH> Default constructors don't take arguments - never.
Sure they do. You said that bar has a default constructor ... but it can
take a parameter.
TH> Try it. Create a class with *one* constructor having one or more
TH> parameters. Specify an instance with no arguments. You will find that
TH> it both compiles *and* runs. Reason, the default constructor that was
TH> ommitted from the code was created by the compiler.
I just did. :-)
DM>JMNSHO.
TH> ???
TH> I'm not up on this acronym. [grin]
Just My Not So Humble Opinion. :-)
---
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)
|