code snippets? Correct?

Snippets create objects below, but I am not sure I know what I am doing completely. Could someone please double check what I am doing?
variation 1
Mid_Points * mp;
Mid_Points * n_ptr = ∓
int result = n_ptr->add(4,9);
n_ptr->print();

variation 2
Mid_Points * mp = Mid_Points();
mp->some member funct

They both work. I can tell with the 2nd its instantiaited on the heap.
But are they correct?
They both work. I can tell with the 2nd its instantiaited on the heap.
But are they correct?

They are not correct.

Variation 1 : You cannot assign a pointer to pointer to Mid_Points to a pointer to Mid_Points variable.

Variation 2 : It is not correct since you cannot construct a pointer with a Mid_Points constructor. You have to use new keyword if that is the case.

In other words, both variations are wrong.
They both work

Are you sure about that? Let's take the first one and try and replicate what you're doing with a POD int:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <vector>

int main()
{
    int* p = new int(9);
  //  int* q = &p;//error: cannot convert 'int**' to 'int*' in initialization|
    int* r = p;
  //  std::cout << "q " << *q << "\n";
    std::cout << "r = " << *r << "\n";
    delete p;
}

Similar to above program when you write Mid_Points * n_ptr = &mp; you should get an error message cannot convert Mid_Points** to Mid_Points* in initialization

Re the second:
I can tell with the 2nd its instantiaited on the heap.

You need to use the new operator to allocate memory first and only then the object's ctor is called. As you have it the ctor is called with allocating memory. So it'd be something like:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

struct Mid_Points{ ~Mid_Points(){std::cout << "Dtor called \n";} void print()const{std::cout << "Printed \n";}};

int main()
{
    Mid_Points * mp = new Mid_Points();
    mp -> print();
    delete mp;
}

Where do I put ctor data:
1
2
3
Mid_Points * mp = new Mid_Points();
    mp -> print();
    delete mp;

My program relies a lot on constructor values ( a lot of them). So I have to create an object that will allow me to to do the constructor thing too...
foo class(){}

Yes, this seems to work fine. No errors thrown.
1
2
3
Corner_Points cp(horizontal,vertical);  //make object w/constructor
    Corner_Points * corn_ptr = &cp;//make pointer to new obj
    cout<<corn_ptr->add(4,3);



7


Similar to above program when you write Mid_Points * n_ptr = &mp; you should get an error message cannot convert Mid_Points** to Mid_Points* in initialization


So I am not saying anybody's wrong here. I just want to know how to create an obj from a class with a ctor list that I can later access with . or ->. That's all. I thought I did with the last lines where I create a pointer to the newly created obj cp, and I was able to successfully access is members.
Last edited on
If I don't, what happens to my ctor values? Do they not get loaded?Are they not there when I try to access them?
1
2
3
4
5
6
7
8
9
10
#include <iostream>

struct Mid_Points{ ~Mid_Points(){std::cout << "Dtor called \n";} void print()const{std::cout << "Printed \n";}};

int main()
{
   /* Mid_Points * mp = Mid_Points();//error: cannot convert 'Mid_Points' to 'Mid_Points*' in initialization|
    mp -> print();
    delete mp;*/
}


1
2
3
Corner_Points cp(horizontal,vertical);  //make object w/constructor
    Corner_Points * corn_ptr = &cp;//make pointer to new obj
    cout<<corn_ptr->add(4,3);

line 1: object created on stack, not heap / OP variation 2 had pointer called ctor without allocating memory through new
line 2; address of object assigned to pointer / OP variation 1 had address of pointer addressed to pointer variable
I just want to know how to create an obj from a class with a ctor list that I can later access with .

search "constructor overloading c++"

I thought I did with the last lines where I create a pointer to the newly created obj cp,

the object cp was created in the previous line and on the stack; the address of this object, &cp, was assigned to the pointer corn_ptr in the following line
Last edited on
Topic archived. No new replies allowed.