variable of another class type

how to initilize a private member of a class which is an object pointer of another class...
**************this code is for pair.h******************************
#include "Box.h"

class Pair
{
public:
Pair(int v1, int v2);
Pair(const Pair& other);
Pair& operator=(const Pair& other);
~Pair();

void show() const { std::cout << "(" << first->value() << ", " << second->value() << ") " << Box::objects() << " boxes" << std::endl; }
void inc() { first-> inc(); second-> inc(); }
private:
Box* first; // please note this is a pointer of another class
Box* second;

};
********************************************************
*********BOX class*********************************************
class Box
{
public:
Box();
Box(int v);
Box(const Box& other);
~Box();
Box& operator=(const Box& other);
int value() const;
void inc();
static int objects();
private:
int _value;
static int _objects;
};
*********************************************************************
have to build a constructor for using pair.h
#include "Pair.h"

Pair::Pair(int v1, int v2)
{
//how to initalize BOX* first and Box* second
}
Look for "Pointers to classes" here: http://www.cplusplus.com/doc/tutorial/classes/

first = new Box();

or

Box temp;
first = &temp;

Don't forget that Box must be defined in your Pair class, either as a subclass or by including it.
Not clear from what you have posted how you want to construct the two boxes in a pair.
However, since you pass v1 and v2 and you have a Box constructor that takes a single value, I'm assuming you want something like this:
1
2
3
4
Pair::Pair(int v1, int v2)
{  first = new Box (v1);
    second = new Box (v2); 
}


Don't forget to delete first and second in Pair's destructor so you don't have a memory leak.

Your copy constructor and assignment operator are going to cause problems if you simply copy the raw pointers. Consider the use of std::shared_ptr.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

@nico
1
2
Box temp;
first = &temp;

That's going to cause problems if temp is a local variable.

Last edited on
@AbstractionAnon, I get the error ' Undefined reference to 'Box::Box(int)'

@nico When i type this :

1
2
3
4
first= new Box();
  first = v1;
  second = new Box();
  second._value = v2;


I get the error : request for member '_value' in '((Pair*)this)->Pair::second', which is of pointer type 'Box*' (maybe you meant to use '->' ?)
second._value = v2;
If the boxes are created within the constructor of pair and are owned by the pair, then they could be members directly, rather than via pointers.

If they are via pointers, but still owned by the pair, then you have to implement custom copy constructor, copy assignment, and destructor for the Pair too (and/or move contructor/assignment).

If they are via pointers, but still owned by the pair, then you should use smart pointers, like std::unique_ptr rather than raw pointers.
1
2
first= new Box(); // This initializes the "first" pointer (changes it from nullptr to point to the new Box object).
first = v1;           //  This will make "first" point to some probably unrelated place in memory, depending on the value of V1. You have now lost your Box object and leaked some memory. 


1
2
3
4
5
6
7
second = new Box(); // This initializes the "second" pointer (changes it from nullptr to point to the new Box object).
second._value = v2; // I think this will not compile, to refer to the _value of an object using a pointer to that object you should use -> instead of a .
// when using a pointer, change this to:
second->_value = v2; // This would assign the value of variable v2 to the _value parameter in the object that "second" points to, IF _value had been public. 
// unfortunately _value is private, so you need to implement something in the Box class. 
// Either implement a public setter function to set _value or implement the Box(int value); constructor and initialize the pointer like:
// second = new Box(v2); 


As a side note: if you are using the actual object instead of a pointer to it, you should use . instead of -> to access its properties/functions.
Undefined reference to 'Box::Box(int)

You do have:
class Box
{
// other code
Box(int v);
// other code
};[/code]
Where is the implementation of that constructor? Your program will not compile before you have one.
Topic archived. No new replies allowed.