Pointer initialization in a class constructor

Hi,

Suppose I have the following class:


1
2
3
4
5
6
7
8
class Closed_chain  {

public:

private:
int element;
int* pt_element;
}


with constructor:
Closed_chain(int element, int* pt_element):element(element), pt_element(pt_element){}

In the main, how can I set the default value of the copnstructor in order to allows me to create a single element chain that contains a pointer that point to its own element.

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class ClosedChain
{
    private:
        int Element;
        int *PtrElement;
    public:
        ClosedChain();
        ~ClosedChain(){};
};

ClosedChain::ClosedChain()
{
    Element = 5;
    *PtrElement = Element;
}


Is this what you are shooting for? The pointer really doesn't do anything other than point to the spot where Element is and gets the value. So, Element would be 5 and *PtrElement would be 5 also. Unless you wanted it to get the location of Element then it would be different. This code will not compile correctly because it is not a whole section of code. This is only the class definition and constructor. In the constructor you can make Element and *PtrElement equal to anything you wish. If Element variable changes than in theory *PtrElement should change as well.
@Khaltazar, I believe you wanted PtrElement = ∈ because otherwise you didn't init PtrElement well and it might cause errors.
Topic archived. No new replies allowed.