class object as argument to member function

Hello,

I want to have a member function that takes as argument another class.
A simple version of the code is:

// THIS IS THE FIRST CLASS
class point{
int l,h;
public:
point(int l, int h){
this->l=l;
this->h=h;
}
};

// THIS IS THE CLASS THAT TAKES AS MEMBER THE FIRST CLASS
class centralize{
point p;
int n;
public:
centralize (point p, int n){
this->n=n;
this->p= p;
}

};

Could you give me some hint on what I am doing wrong?

Thanks,
Bogdan
1
2
3
4
5
6
7
8
class point{
int l,h;
public:
point(int l, int h){
this->l=l; /*<---What is this supposed to accomplish?*/
this->h=h;
}
};


You do the same thing in both constuctors, but I don't get it. If you want the arguments to the consturctors to fill in the data in the private members then you need to call them something else, even capatilizing the arguments like this would technically work.
1
2
3
4
5
6
7
8
class point{
int l,h;
public:
point(int L, int H){
this->l=L;
this->h=H;
}
};



But as it stands now you are telling the computer that "this data is equal to itself" which is silly.
Last edited on
You defined a constructor for your point class, so the default constructor is no longer provided for you. You must create it yourself.

Add a default constructor to your point class:

1
2
  point()
    {};



Your centralize class might need one as well, depending on how you ever create it. The following code works. Experiment by adding and removing default constructors, and see if you can figure out where the default constructor is being called.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <cstdio>

// THIS IS THE FIRST CLASS
class point{

public:
int l,h;
point(int l, int h){
this->l=l;
this->h=h;
printf("Calling non-default ctor on point... \n");
}
point()
{printf("Calling default ctor on point... \n");};
  
};

// THIS IS THE CLASS THAT TAKES AS MEMBER THE FIRST CLASS
class centralize{
point p;
int n;
public:
centralize (point p, int n){
this->n=n;
this->p= p;
printf("%i ",p.l);
printf("%i", this->n);

}

};

int main()
{
  point A(3,4);
  centralize B(A,7);
  return 0;
}
Last edited on
@Computergeek01: I think you are wrong. When you have several variables with the same name (but different scope) you will refer to the closest scope.
1
2
3
4
int x; //global variable
void foo(int x){
  ::x = x; //assign to the global variable the value of the parameter
}


If you don't want a default constructor use a initialization list
centralize::centralize( point p, int n ): p(p), n(n) {}
You are correct (EDIT:In your scenario), again I don't know what the OP was trying to do. I assumed that since he had private varaibles with the same names as his arguments in the class that he was writing the constructor for that he wanted to assign the arguments to that constructor to those varaibles. Again I'm guessing at the OP's intent so it's very possible that I'm wrong.
Last edited on
Hello

Computergeek. :I intended to assign the arguments to the class variables. And yes, using a different name for the arguments would have done also the job. But the "this" pointer allows to do the same.

Moschops: I got the ideea with the constructor even if I don't see why I have to have the default constructor. It works.

Thanks
So you know enough to write code that is so awkard to read that actually annoys people who are trying to help you, but you can't figure out why your constructor isn't working? Whatever. I'm done with this crap today.
I got the ideea with the constructor even if I don't see why I have to have the default constructor.


You now see where in your code the default constructor was being called. If no default constructor is provided, how can you call it? How can you call a function that doe snot exist? You can't, which is why your original code didn't work.

If you don't define any constructors, a default constructor is made for you. If you define any constructors, the default constructor is not made for you, so if you want to use it (which you did in your code) then you will have to make it yourself.

It is really, really useful to know when constructors and destructors are called. Definitely worth learning.

Topic archived. No new replies allowed.