Hello,

I have been given a class diagram where a variable called light_years must be a const int and stored in the space class, but later on that value must be used in the main file.

Below is the space class

class space
{

private:

    static const int LIGHT_YEARS;

public:

    // -Many functions are here-

};

I need to access the value here

#include "stdafx.h"
#include <iostream>
#include "Space.h"

int _tmain(int argc, _TCHAR* argv[])
{
    for (int i=0; i<3; i++)
    {
            8*LIGHT_YEARS;  // I've tried 8*Space::LIGHT_YEARS;
    }

    system("pause");
    return 0;
}

Is there someway to friend the main function or how can I go about doing this? apeciate any help!

Recommended Answers

All 3 Replies

You could write a get method(a public interface method) to obtain the private member perhaps?

Yeah, I don't see any other way than what @myk45 said. It's a static variable, meaning it's scope is the class only.

Does the constant have to be private? It seems silly to require it to be private and also require it to be accessed directly from outside of the class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.