• Advertisement
Sign in to follow this  

how to pass a name

This topic is 4794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok i have a problem here. I want to be able to to create a function for a class the in the constructor pass and name for the class i am create. to try to show you want i me hey is what i what to be able to do, something like this: i have a class cam point and the defualt count i give x and y but i also what to pass a name and in the function give it the name, some like this: construtor: Point::Point( int x, int y, string(or whatever) name) { Point *name(the passed value in the parameter) = new Point( x, y, "" ); } and that function happens when i call Point( 8, 8, "pointa" ); is it possible using the construtor? is it possible any other what?

Share this post


Link to post
Share on other sites
Advertisement
I seriously doubt that you can use a constructor.

None the less, to do what you've asked, it's like this:

#define createPoint(x,y,name) Point* name = new Point(x,y,"");

Share this post


Link to post
Share on other sites
I don't think you've thought this through.

You wouldn't be able to refer to the variable in your code because you wouldn't know what it was going to be called. The point of variables is they vary!

Why would you need to do it?

What would the code further on in the function look like? It doesn't make any sense.

Share this post


Link to post
Share on other sites
Or if I've misunderstood you

//Point.h
#include <string>

class Point {
std::string m_name;
int m_x,
m_y,
m_z;
public:
Point(int x, int y, const std::string& name)
: m_x(x), m_y(y), m_name(name) {
}
const std::string& Name() const {
return m_name;
}
}




Usage:

#include <iostream>
#include "Point.h"
int main() {
Point point(2, 5, "pointa");
std::cout << point.Name() << std::endl;
return 0;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement