constructor that initializes a pointer to point to the object

I have a program that has a base class 'control' and there are 2 dervied classes 'button' and 'textbox'.
How do i make a constructor in the 'button' or 'textbox' that initializes a pointer of the data type 'control' to point to the object that invokes the constructor
the code should look like this
1
2
3
4
5
6
7
8
9
10
11
class control
{
   //data
}
class button:public control
{
   buton()
   {
        //code for the constructor
   }
}

actually i have an array of pointers of the type 'control' and as soon as any instance of a control like button or textbox is created the constructor should make an element of the array to point to the instance
letscode wrote:
as soon as any instance of a control like button or textbox is created the constructor should make an element of the array to point to the instance
You shouldn't do this. Why do you need to?
the class 'control' has the data member 'tabstop' specified by the programmer. when the user presses tab key, the control having next tabstop should get highlighted (similar to the tab keypress in windows). For this i need an array that points to the instances of the controls when they are created.
I think what you are referring to is aggregation and inheritance. This lets you define a control, then you overload virtual functions to customize it and make the button or textbox.
1
2
3
4
std::vector<control*> myControls;

myControls.push_back( new button() );
myControls.push_back( new textbox() );


You also mentioned making a pointer to the object that invokes the constructor. I'm guessing that you're talking about a parent window. That could be done like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
class control
{
private:
    control* m_parent;
public:
    control(control* parent = 0) : m_parent(parent) {}
}

class button:public control
{
public:
    button(control* parent = 0) : control(parent) {}
}

then to create: control* SomeControl = new button(this);



Last edited on
well that was a little beyond my knowledge, can you please explain your code. Or is there any other solution to this problem of 'tab' key
I don't think tabstop should be a member of the control class.
Yes it is definitely necessary in 'control' bcoz all other controls like button and textbox are derived from control
I mean tabstop should be a secondary property - it should be a result of the order in the vector container, not the result of a value contained in each control.

If for some reason you still want this property to be a primary property, I think boost has a map that does this for you (it lets you map by a data member in the object).
Topic archived. No new replies allowed.