2

I'm in a function in java and I create a new Object passing "this" as parameter:

class AClass { 
    AClass(TestClass testClass) { }
}

class TestClass {
    AClass doSomething()
    {
        return new AClass(this);
    }
}

How to do That in C++?

Should be:

class AClass {
    AClass(TestClass* testClass) { }
};

class TestClass {
    AClass* doSomething()
    {
        return new AClass(*this);
    }
};

Should I pass *this, or &this?

roalz
  • 2,699
  • 3
  • 25
  • 42
okami
  • 2,093
  • 7
  • 28
  • 40
  • 6
    You should get [a good introductory book](http://stackoverflow.com/questions/388242/the-definitive-c++-book-guide-and-list) if you don't have one. Aside from the use of mustaches (sorry, I mean curly braces), C++ is not very similar to Java at all. – James McNellis Sep 01 '10 at 03:26

2 Answers2

1

It depends. You're probably looking for this:

class AClass { 
    AClass(TestClass& testClass) { } 
}; 

class TestClass { 
    AClass doSomething() 
    { 
        return AClass(*this); 
    } 
}; 

To use it in C++:

TestClass testClass;
AClass aClass = testClass.doSomething();

But what are you really trying to do? Unlike Java, C++ makes the distinction between values and references explicit. You should really read a good beginner's C++ book, as James McNellis has suggested.

The distinction between values/references/pointers that C++ makes is fundamental to the language and failing to respect that will lead to disaster. Again, please pick up a C++ book.

Community
  • 1
  • 1
In silico
  • 51,091
  • 10
  • 150
  • 143
  • by this way you're copying the TestClass passing *this? So the constructor is being called like that: AClass(TestClass testClass) { } but if I put "return &this;" the AClass constructor should be: AClass(TestClass *testClass) { } ? – okami Sep 01 '10 at 03:29
  • Again, that depends, since C++ makes the distinction between values and references explicit. Passing `*this` to `AClass(TestClass testClass)` makes a copy, while passing `*this` to `AClass(TestClass& testClass)` doesn't. That is one of the fundamental differences from Java - I recommend a good C++ book as it is much beyond the scope of this answer. – In silico Sep 01 '10 at 03:32
  • I want to pass the TestClass as reference – okami Sep 01 '10 at 03:34
  • @okami - I have modified my answer so that a reference to `TestClass` is passed instead of a pointer. But please, I implore you to pick up a C++ book and read it (see http://stackoverflow.com/questions/388242/the-definitive-c++-book-guide-and-list). I don't mean to be offensive, but C++ is very different from Java and treating C++ like Java will not help you in the long run. – In silico Sep 01 '10 at 03:37
  • 1
    `this` is of type `TestClass *` so doing `AClass(this)` will call `AClass(TestClass *testClass)`. – Niki Yoshiuchi Sep 01 '10 at 03:39
  • @NikiYoshiuchi: I can't believe that you're the only one who gave this correct answer! ('this' is *already* a pointer to TestClass). – Dave Doknjas Aug 09 '15 at 17:12
0

Given the declaration you wrote

class AClass {
    AClass(TestClass* testClass) { }
};

You need to pass a pointer, so you'd use this. However, it's generally preferred in C++ to use references (especially const references) instead of pointers, so you'd declare AClass as:

class AClass {
    AClass(const TestClass& testClass) { }
};

To which you would pass *this.

Now, there are situations in which the pointer version is preferred. For example, if testClass were allowed to be NULL (you can't have null references). Or if testClass were stored in a std::vector or similar data structure. (You can't have arrays of references.)

dan04
  • 87,747
  • 23
  • 163
  • 198