0

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!

4
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by deceptikon
0

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

0

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.

Edited by phorce

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.