0

When we are coding with java we can have reference variables that are not assigned to any object. Example : Player p1 here p1 is assigned only if we create an object like below : p1 = new Player(); Here p1 stores the address of the newly created Player object.

If we create a variable in c++ like this, it is also creating a new object. Example : Player p2 p2 here assigned to a new object. That way we cannot have any variable only of some type.

Also we can create pointers to the object as following, Player* p3 = new Player(); If both(p2 & p3) are holding addresses,

i) What use of pointer(p3) of that type?

ii) How can we just create variables to a type ?

asinduvg
  • 67
  • 9
  • 1
    *If both(p2 & p3) are holding addresses...* They're not. `p2` IS an object of type Player.. – jrok Nov 18 '19 at 08:09
  • `p2`does not hold a adress to an object. It holds the complete data of the object on the stack. – RoQuOTriX Nov 18 '19 at 08:09
  • 2
    Does this answer your question? [in C++, what's the difference between an object and a pointer to an object?](https://stackoverflow.com/questions/5372707/in-c-whats-the-difference-between-an-object-and-a-pointer-to-an-object) – Daniel Langr Nov 18 '19 at 08:13
  • `Player p1` in Java is very loosely like `Player* p1` in C++. But it's not a 1-to-1 equivalence, and it would be wise to learn pointers and references in C++ without thinking too much about Java. – Max Langhof Nov 18 '19 at 08:20
  • The nearest thing to a Java object in C++ is a `std::shared_ptr`. – Bathsheba Nov 18 '19 at 08:37
  • @jrok So that way we cannot create just variables of type Player? – asinduvg Nov 18 '19 at 08:40
  • If I want to create just the variables, only way to do it in c++ is to create a pointer of the type like ```Player* p```. Am i right? – asinduvg Nov 18 '19 at 08:55
  • *"I want to create just the variables"* Then you want `Player p2;`. – HolyBlackCat Nov 18 '19 at 09:58
  • @Asindu_vg Both creates a value. `p2` creates a `Player` and `p3` creates a pointer-to-player. The pointer can point to nothing, or it can point to an object. – super Nov 18 '19 at 10:00
  • @Asindu_vg: I'm not sure what you mean by "just the variables". Are you talking about a variable of type pointer to Player, and not set it to point to anything? Similar to just writing `Player p1;` in Java? That would be `Player *p4;` in C++. – Thomas Padron-McCarthy Nov 18 '19 at 15:20

2 Answers2

2

p2 doesn’t hold an address. It contains the actual object, in the variable itself.

Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75
0

The closest you can get to mimic what Java does is (but see below about shared_ptr):

std::shared_ptr<Player> p;
assert(not p);  // Assertion holds!
p = std::make_shared<Player>();
assert(p);  // Assertion holds!

That’s roughly equivalent to:

Player* p = nullptr;
p = new Player;
// ...
delete p;

But that’s manual memory management, which is highly error prone. Don’t do it!

If you don’t have a pointer to an object, but just the object itself (a value in C++ terminology), you lose the empty state.

Player p;

p is the object itself. That line creates the object and there’s no way around it. There’s no indirection which before gave you the opportunity to represent “no object present”. You can add an empty state back, if you need it:

std::optional<Player> p;  // since C++17, before use boost::optional
assert(not p); // holds
p = Player();
assert(p); // holds

A note about shared_ptr. Real shared ownership is actually pretty rare. Usually you have (and want to have) exactly one owner of an allocated object. So, in most cases you want unique_ptr (if you want a pointer at all):

std::unique_ptr<Player> p;
assert(not p);  // holds
p = std::make_unique<Player>();
assert(p); // holds
besc
  • 2,507
  • 13
  • 10