Joel
Joel

Reputation: 2035

What's the name in programming of this?

I've seen this in wxWidgets and other C++ code style:

#include <iostream>

class Foo {
    int _x;
    int _y;
    public:
        Foo () : _x(0), _y(0) {}
        Foo (int x, int y) : _x(x), _y(y) {}
        int get () const { return _x+_y; }
};

int GetFoo (const Foo& f) {
    return f.get ();
}

int main () {
    Foo f1(2,3); // create object with values
    std::cout << "Value1: " << f1.get () << '\n'; // access object method
    std::cout << "Value2: " << GetFoo(Foo(3,7)) << '\n'; // How do you call this?
    return 0;
}

Is when you use direct class with some values. I name it pass direct class as object but I'd like to know the propper term. Thanks.

Upvotes: 2

Views: 63

Answers (2)

Captain Giraffe
Captain Giraffe

Reputation: 14705

The function

int GetFoo (const Foo& f) {
    return f.get ();
}

Takes a constant reference (const Foo& ). The term reference is c++ specific. The general term is an alias; two names describing the same instance. The const qualifier also allows for implicit conversions to take effect.

GetFoo(Foo(3,7))

Is creating a temporary object that lasts for the duration of the function call. The temporary Foo(3, 7) has no name, but it is being aliased or referenced in the function.

Upvotes: 1

Sam Varshavchik
Sam Varshavchik

Reputation: 118330

You're probably looking for the term "temporary value", or "a temporary".

This generally describes an object that gets instantiated for the duration of executing a single expression, or a part of it, after which the object gets immediately destroyed.

That's what's happening here.

Upvotes: 5

Related Questions