Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructors with default parameters in Header files

I have a cpp file like this:

#include Foo.h; Foo::Foo(int a, int b=0) {     this->x = a;     this->y = b; } 

How do I refer to this in Foo.h?

like image 889
royvandewater Avatar asked Sep 17 '09 17:09

royvandewater


People also ask

Do you put constructors in header files?

In both cases the constructor is inline. The only correct way to make it a regular out-of-line function would be to define it in the implementation file, not in the header.

Do constructors have parameters by default?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

How many types of default constructors are there?

Default Constructors in C++ They are primarily useful for providing initial values for variables of the class. The two main types of constructors are default constructors and parameterized constructors. Default constructors do not take any parameters.


1 Answers

.h:

class Foo {     int x, y;     Foo(int a, int b=0); }; 

.cc:

#include "foo.h"  Foo::Foo(int a,int b)     : x(a), y(b) { } 

You only add defaults to declaration, not implementation.

like image 125
Michael Krelin - hacker Avatar answered Sep 24 '22 03:09

Michael Krelin - hacker