简体   繁体   中英

How to access the variable from another function in a class

So I have experience using programming languages and just switched over to C++. Now I have created a few working applications but I always stumble upon the same problem. I don't exactly know what everything is called in the code. You have a class which obvious to see since the has written class before it. And you also have some sort of functions attached to the class are these called instances? And is the class the object it referring to by for example class::function.

But my main question was how can I access the variables from another function within the same class file. I have included an example below explaining what I want to achieve. I already tried a lot of things and googled a lot. I tried code pasting code creating setting and getting functions, calling the class to get and set the variable but I can't get it to work. I've spend a lot of time fixing this very basic problem. Could someone explain me what is called what in this code (Class,Object,Instance). And explain me the most efficient way to receive data from another function in the same .cpp file.

Thanks

load_data.h

#pragma once

class load_data
{

public:

    static int data[13];

    load_data();
    static void test2();

};

load_data.cpp

#include "load_data.h"
#include "abc.h"


load_data::load_data()
{
    int data[3]; // Initializing the array

    data[0] = abc::LoadImage("textures/1.png");
    data[1] = abc::LoadImage("textures/2.png");
    data[2] = abc::LoadImage("textures/3.png");
}

void load_data::test2()
{

    abc::CreateSprite(1, data[0]); 
    abc::SetSpritePosition(1, 0, 0);
    abc::SetSpriteScale(1, 3, 3);

// Now I get an error saying it has no data. Which however is set in
// load_data(). But since each function gets its own variables this one will be empty.

    abc::CreateSprite(2, data[1]);
    abc::SetSpritePosition(2, 64, 64);
    abc::SetSpriteScale(2, 3, 3);

    abc::CreateSprite(3, data[2]);
    abc::SetSpritePosition(3, 128, 128);
    abc::SetSpriteScale(3, 3, 3);

}

Change your load_data() constructor to the following (currently, your creating a new data[] variable that is locally scoped to your load_data() constructor, which gets initialized instead of your object's data[] (gets "eclipsed"). Your subsequent call to test2() fails when it accesses data[] because the other, local data[] was initialized instead. Also, fyi, the local data[] is destroyed when load_data() returns (because it is an auto/stack variable that falls out of scope).

load_data::load_data()
{
    //int data[3]; // Initializing the array

    data[0] = abc::LoadImage("textures/1.png");
    data[1] = abc::LoadImage("textures/2.png");
    data[2] = abc::LoadImage("textures/3.png");
}

you also have some sort of functions attached to the class are these called instances?

An object is an instance of a class . A class defines the object type, which consists of state variables, and operations for them (known as "member functions" or "methods").

An object's methods are called through a handle to the instance. IE, an instantiated variable:

load_data ld = new load_data();
ld.test2();

And is the class the object it referring to by for example class::function.

This notation is for explicitly qualifying a method name. It helps resolve naming conflicts and should only be used when needed (otherwise it is implicit).

But my main question was how can I access the variables from another function within the same class file. ... But since each function gets its own variables this one will be empty

All functions of a class share the class'es (member) variables. An given instance of the class has the only copy of those member variables (ie, their specific values/memory to that instance), so all method calls through a specific instance variable (say ld ) of load_data will refer to the same data[] array (so load_data ld1, ld2 would each have their own copies). Functions can, and usually do, declare their own variables to help assist in computing the task they perform (bools, counters, etc...). These such variables, as mentioned before, are scoped to that function, meaning they're no longer allocated and get destroyed when the function returns (they are auto-variables).

And you also have some sort of functions attached to the class are these called instances?

No. the functions inside of the class are called "class member function" or just "member functions". An instance is a copy of that object (read class) in memory. So in short:

class A {
public:
  void fun (void); ///< This is a class member function
};

void main (int argc, char *argv[]) {
  A a; ///< a is an instance of object A
}

And is the class the object it referring to by for example class::function.

The class defines the object. In the above snipped, A is an object.

But my main question was how can I access the variables from another function within the same class file.

You need to do some reading on variable scope. In your above example the data array is local to the constructor. It doesn't exist within the object, only within that function. So as soon as the constructor finishes, the variable goes out of scope and is lost. In order to keep it in the object's scope you would need to declare it within the object.

class load_object {
public:
  // The same
private:
  int load[3];
};

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM