Can I set a class member variable to the result of a class function?

Related searches

I have two functions in a Vector class, x() and y() that return the corresponding component from the vector. I want to be able to access the component using a member variable instead (or as well).

Previously I made x and y pointers that point directly into the array of components, data - but this isn't a good solution because someone could change the pointer, right? And it would need to be dereferenced every time it is used.

I cannot use a union due to how my classes are structured.

Currently my idea is to have the member variables x and y return the result of functions x() and y(). Is this a feasible solution?

template <typename T>
class Vec2 : public VecBase<T, 2>
{
public:
    T& x = x();     //Doesn't work but is what I'm trying to achieve
    T& y = y();

    T& x() const { return data[0]; }
    T& y() const { return data[1]; }
}

EDIT: Should have made it more obvious - VecBase is a variable sized vector (vector2, vector3, etc.). Vec2 adds x and y when the size is 2 to make accessing the components easier. It's hard to show without pasting all the code. Ha.

The outcome I want is to basically be able to access the components of the vector easily like so:

Vec2<int> vector;
vector.x = 20;
vector.y = 40;

The functions x() and y() aren't necessary in the solution.

Thanks for your help!

Your reference member approach can work:

template <typename T>
class Vec2 : public VecBase<T, 2>
{
public:
    Vec2() : x(this->data[0]), y(this->data[1]) {}

    T& x;
    T& y;
};

However, now you have a user-defined constructor. Plus there's still going to be a dereference under the bonnet. Personally I'd avoid this over-complication entirely, if possible.

Although I understand that you want to implement the guts of it all in a generic way, and that your base can't "automatically" name the members in a nice mathsy way, consider whether you really need this. Why not just a couple of Vec2, Vec3 types that literally just have direct members in them? Do you really need anything more?

Having T as a template parameter is a good idea though.

8.2 — Classes and class members, To actually create an object of the class, a variable of that class type must be defined: Member functions can be defined inside or outside of the class definition. Output 7 123 7 123. In such a context, the class name effectively acts like a� Which can be instantiated by: new KeyEvent(MyFunction); Where MyFunction is the name of some function in that class. The KeyEvent class can then invoke that function with: CallBackFunction(); The Action class also allows strongly typed parameters to be passed in and the Func class can be used for methods that return a result.

I want to be able to access the component using a member variable

Then simply use a struct (or class with public members):

template <typename T>
struct Vector
{ 
    T x;
    T y;
};

Vector<int> v;
v.x = 42;
v.y = data[2];

8.11 — Static member variables, Member variables of a class can be made static by using the static keyword. This program produces the following output: Do not put the static member definition in a header file (much like a global Just need clarity if really makes sense to apply const to static member functions/objects in a class and why const can't be� Member variables are known as instance variables in java. Instance variables are declared in a class, but outside a method, constructor or any block. When space is allocated for an object in the heap, a slot for each instance variable value is created.

You can achieve almost what you want without the reference members. You can assign to the result of x().

Vec2<int> vector;
vector.x() = 20;
vector.y() = 40;

Note that you shouldn't allow modifying through a const Vec2, so overload x and y based on the const qualification.

template <typename T>
class Vec2 : public VecBase<T, 2>
{
public:
    const T& x() const { return data[0]; }
    const T& y() const { return data[1]; }

    T& x() { return data[0]; }
    T& y() { return data[1]; }
};

C++ Class Member Functions, C++ Class Member Functions - A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. Member functions can be defined within the class definition or separately using If you like, you can define the same function outside the class using the scope� Now, remember that each instance of a class gets its own copy of the member variables, unless the variables are static. But functions are shared throughout the class. Yes, you can distinguish static functions from nonstatic functions. But that just refers to what types of variables they access: Static functions can access only static member variables, and you don’t need to refer to them with an instance.

Class basic syntax, The constructor() method is called automatically by new , so we can initialize the object there. is to put a comma between class methods, which would result in a syntax error. We can illustrate the result of class User declaration as: state for JavaScript classes such as private variables and methods. In the previous chapter, you learned that variables inside a class are called fields, and that you can access them by creating an object of the class, and by using the dot syntax (.). The following example will create an object of the Car class, with the name myObj. Then we print the value of the fields color and maxSpeed:

Instance and Class Members, You can access class variables through an instance or through the class itself. the instance variables declared within the class (unless they create a new object and All instances of a class share the same implementation of an instance method; sets their x values, and then displays them produces this, different, output. To capture the member variables inside lambda function, capture the “this” pointer by value i.e. std::for_each(vec.begin(), vec.end(), [this](int element){ //. Capturing this pointer inside lambda function will automatically capture all the member variables for this object inside lambda.

A variable provides us with named storage that our programs can manipulate. Java provides three types of variables. Class variables − Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.

Comments
  • Unfortunately, C++ does not work this way. At least not without a massive amount of overloading and various other things that only create confusion and results in extremely convoluted code. Don't try to make C++ work like some other programming language. A class method is a class method. A class member is a class member. They don't really want to be each other.
  • Is it necessary to save it in an std::vector as well? Another comment on how you want to access the variables, are you sure you don't want to use a get- and set-function? In my eyes, please correct me, this would violently break the paradigm of data encapsulation.
  • Don't use references here, you're wasting memory for syntax sugar. It's a bad idea. Instead, do T x, y; and overload operator[] for array-like access.
  • @skratchi.at The base class VecBase allows for a variable sized vector (vector2, vector3, etc.) The Vec2 class extends the functionality of that by allowing easy access through .x and .y when the number of components is 2 - that's the plan at least.
  • Aside: T& x() const is rather suspicious. Why can you modify part of a const Vec2? prefer const T& x() const; T& x();
  • Well that could work! Thanks. The reason I have a base class is because there's going to be more behaviour (normalisation, dot product, cross product, operator overloading) etc. and I am trying to avoid re-writing functions in multiple classes that are only slightly different.
  • @Davinatoratoe Don't those operations have to be different for different orders anyway?
  • No, I have most of them working in any order so far :)
  • Yeah, this is for an assignment that requires accessing the components via a variable, not a function. :( I'd love to do it that way if I could haha