0

Possible Duplicate:
c++ call constructor from constructor

How to do "self" (this) assignments in c++?

Java:

 public Point(Point p) {
        this(p.x, p.y);
    }

How would do this in C++?

Would it be similar only this->(constructor of point that takes x, constructor of point that takes y);?

Community
  • 1
  • 1
nobody
  • 229
  • 1
  • 2
  • 6

3 Answers3

8

In C++0x, you can use delegating constructors:

Point(const Point &p) : Point(p.x, p.y) { }

Note that no compiler has full support for C++0x yet; this particular feature is not yet implemented in G++.

In older versions of C++, you have to delegate to a private construction function:

private:
    void init(int x, int y) { ... }
public:
    Point(const Point &p) { init(p.x, p.y); }
    Point(int x, int y)   { init(x, y); }
bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • 2
    Though in this case it's probably not worth the hassle. – GManNickG Aug 02 '11 at 21:45
  • 1
    I don't get the comment about *older* versions. It was always a design error using *init* and similar functions. – Gene Bushuyev Aug 02 '11 at 21:46
  • 1
    Though be careful -- delegating constructors aren't implemented in g++ yet even with --std=c++0x. I didn't realize that for a while once... – Sean Aug 02 '11 at 21:47
  • @Gene, C++0x allows you to directly delegate to a different constructor. This is a new feature; the first code sample will not compile on compilers that do not support C++0x – bdonlan Aug 02 '11 at 21:47
  • 1
    @bdonlan -- I know about delegating constructors, I'm saying using *init* type of functions was always a bad design. In pre-C++0x, one would write a non-delegating ctor, like `Point(const Point& p) : x(p.x), y(p.y) {}` even when it means more typing. – Gene Bushuyev Aug 02 '11 at 21:49
  • 1
    @Gene, for trivial constructors, this works, but if you're doing anything overly complex in the constructor, it may be justified to factor it out into a common function. It always depends on circumstances, then. – bdonlan Aug 02 '11 at 21:51
  • @bdonlan -- though I can't claim it's *never* necessary, I can't remember a case where it would be so needed as to go against good design practices. After all, you can achieve the same with another object creation and swap, which is a less tainted design. – Gene Bushuyev Aug 02 '11 at 21:58
  • @Gene, since you're in your constructor, there's no guarentee that you're in a condition where swap is safe :) – bdonlan Aug 02 '11 at 22:00
  • @bdonlan -- no more unsafe than calling any other function, e.g. *init* – Gene Bushuyev Aug 02 '11 at 22:08
  • @Gene, indeed, my point is that `init` deals with a single precondition (class is uninitialized), and with an `init` you have a `swap` with an equally simple precondition (both objects are initialized). If you're going to be passing partially initialized structures to swap, now swap has to deal with partially initialized structures as well as fully initialized structures. You'll also fully initialize data members and superclasses twice. – bdonlan Aug 02 '11 at 22:09
2

If I understand what you mean by this Java code (a constructor that relies on another constructor of the same class to do the job):

public Point(Point p) {
    this(p.x, p.y);
}

this is how I would express the same in C++:

class Point {

    Point(const Point& p)
       : Point(p.x, p.y) 
    {
        ...
    }
};
sergio
  • 68,819
  • 11
  • 102
  • 123
0

If you call another constructor of the same class it will create a new object.

If you want to do this, you should put the constructor logic in an init method and call it from all constructors.

SJuan76
  • 24,532
  • 6
  • 47
  • 87