how to use class variable in class ?

mc 5,266 Reputation points
2025-05-02T11:44:30.7833333+00:00

I want to use variable in class

class Path{
public:
float get_x(){
int x=0;
for(int i=0;i<ARRAYSIZE(Paths);i++){
	if(Paths[i].r){
		return Paths[i].id;
	}
}
}
}
Path Paths[30];

but there will be error right? I can not use Paths in class

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,921 questions
0 comments No comments
{count} votes

Accepted answer
  1. Darran Rowe 1,616 Reputation points
    2025-05-02T12:31:58.32+00:00

    It is better to make this a templated static member. The array can then be passed in as a parameter.

    class path
    {
    public:
    	template <unsigned int N>
    	static int do_stuff(path(&paths)[N])
    	{
    		return paths[0].id;
    	}
    private:
    	int r;
    	int id;
    };
    
    int wmain()
    {
    	path paths[20];
    
    	path::do_stuff(paths);
    	return 0;
    }
    

    It would be even better to not use C style arrays and instead use std::array or std::vector.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.