0

I've been searching across the web but seen to not find the keyword. let's say I want to use the class type to create a method/function. Here is an easy example:

struct action{
//constructor
action(int n){
}
};
action move(){
}

here, I'm using the action class as the type of the function. Here are my questions: What is this called? How do I use the constructor of the class? what should I return in the function move? (it doesn't let me return this. error:[invalid use of 'this' outside of a non-static member function])

john
  • 85,011
  • 4
  • 57
  • 81
Eden Cheung
  • 303
  • 2
  • 8
  • 2
    Your `move` function looks like a normal non-member function, with `action` as the return type. And as a non-member function it of course can't use `this` since it's not member of any object that `this` can point to. – Some programmer dude Jul 22 '19 at 10:22
  • 1
    It's just a function. Theres nothing special about it. Your error suggests that you used `this` in `move()` which is outside the class. So its a standalone function. Its not a method, and so `this` doesn't make sense. – freakish Jul 22 '19 at 10:23
  • 2
    And it seems to me that you could use [a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read. – Some programmer dude Jul 22 '19 at 10:25
  • 1
    Edited the code to include an `int` argument for the constructor. See OPs comments below. – john Jul 22 '19 at 10:36

2 Answers2

2

There's no special name for this situation. It's perfectly common.

You call the constructor in all the usual ways, e.g

action move() {
    return action(42);
}

or

action move() {
    action a(42);
    return a;
}

In your code (and my answer) move is a normal function. Maybe you meant it to be a member function, in which case you could also return *this; to return the current object.

john
  • 85,011
  • 4
  • 57
  • 81
2

What is this called?

move is the name of a free function. The full signature action move() tells you that its return value is an instance of type action and that the functions doesn't expect any parameters. Note that free functions are different from member functions in that they don't have a special relationship to any class.

How do I use the constructor of the class?

The constructor is called when you create an instance of that class. Example:

action instance; // calls default constructor

Note that you don't really invoke constructors directly. In the above case, it's a declaration that leads to a call to action::action().

what should I return in the function move?

An instance of action, because the function signature says so:

action move() { return action{}; }

If your constructor takes parameters, here's an adjusted example:

struct action {
    action(int n) { /* do stuff with the argument... */ }
};

action move() { return action{42}; }
lubgr
  • 37,368
  • 3
  • 66
  • 117
  • can you give me an example of how to use the constructor? say the constructor takes in an int – Eden Cheung Jul 22 '19 at 10:33
  • Im sorry but im very new to this... i thought `action move` is an object of the class `action`. If so, why can't it "return itself" as an object? – Eden Cheung Jul 22 '19 at 10:39
  • @EdenCheung Because you declared it **outside** of the action class. Put it inside if you want it to be a memory function. – john Jul 22 '19 at 10:41
  • @EdenCheung It helps to understand these things if you indent your code poperly. With proper indentation you would easily see that `move` is outside of the `action` class. – john Jul 22 '19 at 10:43
  • I cannot really wrap my head around this. Let me tell you what i originally want this to do. So in the class `action`, i have some variables and some member functions. I want to create objects like move etc. and i need to access the member objects and methods in the action class. – Eden Cheung Jul 22 '19 at 10:49
  • 1
    The way you have written the code above `move` is a function not an object. Really does sound like you need to read a good C++ book. C++ is difficult, you can't guess. – john Jul 22 '19 at 10:50
  • I do have the book "The C++ Programing Languge". Need to swallow the book over and over ;) – Eden Cheung Jul 22 '19 at 10:55
  • @EdenCheung You mean Stroupstrup's book? That's not a book for beginners. I would look at the list linked to in the comments above. – john Jul 22 '19 at 11:04
  • i do understand his book, but i did not read it from start to end – Eden Cheung Jul 22 '19 at 11:11
  • Let me try to explain what my problem actually is. First, what is the difference between `action move(){}` and `action move{}`? I know its a function and an object, but does the function need a constructor? – Eden Cheung Jul 22 '19 at 11:14
  • @EdenCheung No functions don't need constructors because they aren't objects, they just need to be defined. (though there are various function-like objects in C++, but regular functions are not objects) – john Jul 22 '19 at 12:56
  • Is it possible to use the constructor in a function? – Eden Cheung Jul 22 '19 at 13:13
  • Now i understand. So functions have different properties than objects. – Eden Cheung Jul 22 '19 at 13:14