lundi 14 avril 2014

classe - accès C++ à des variables publiques dans la méthode virtuelle - Stack Overflow


In c++ I have an Abstract class A:


 class A
{
public:
A(){}
virtual ~A(){}
virtual void out(std::ofstream& outFile) = 0;
virtual void in(std::ifstream& inFile) = 0;
};

And B class in which I want to use virtual methods:


class B : public A
{
public:
B();
double angle;
int index;


virtual void out(std::ofstream& outFile);
virtual void in(std::ifstream& inFile);

};

Now here is a question: I want to use B class variable like (int index) in virtual methods:


virtual void out(std::ofstrea& outFile)
{
outFile<< index << angle;
}

Here I get compiler error that angle and index are not declared in this scope. It is somewhat logical because this variables are not declared in class A. Now how can I get access to variable angle and index in virtual methods?


Thanks for your help.




If you're implementing the method outside the class, you don't mark it as virtual and you qualify its name:


void B::out(std::ofstrea& outFile)
{
outFile<< index << angle;
}



When defining your functions out of line you want to use something like this:


void B::out(std::ofstream& outFile)
{
outFile<< index << angle;
}

Any member of the respective class is accessible in a member function, independent of whether the function is virtual or not.


Since talking about functions reading or writing data: Unless you really need to make use of specific details of std::ofstream or std::ifstream, e.g., use open(), you should probably pass std::ostream or std::istream references. This way the functions can also be used with different stream stream types, e.g., std::ostringstream and std::istringstream.




Unless you have written your function body in the class declaration, you must prefix the member function name with the class name:


void B::out(std::ofstream& out)
{
out << index << angle;
}

What exact error did you get by the way, this would be much simpler if you could post the exact error code.




You are missing the class name before member function definition.


void B::out(std::ofstrea& outFile)  // Virtual keyword is optional in defintion.
{
// ...
}



When you define a class method in your cpp file you must give the class name as scope like this :


void B::out(std::ofstrea& outFile)
{
outFile<< index << angle;
}


In c++ I have an Abstract class A:


 class A
{
public:
A(){}
virtual ~A(){}
virtual void out(std::ofstream& outFile) = 0;
virtual void in(std::ifstream& inFile) = 0;
};

And B class in which I want to use virtual methods:


class B : public A
{
public:
B();
double angle;
int index;


virtual void out(std::ofstream& outFile);
virtual void in(std::ifstream& inFile);

};

Now here is a question: I want to use B class variable like (int index) in virtual methods:


virtual void out(std::ofstrea& outFile)
{
outFile<< index << angle;
}

Here I get compiler error that angle and index are not declared in this scope. It is somewhat logical because this variables are not declared in class A. Now how can I get access to variable angle and index in virtual methods?


Thanks for your help.



If you're implementing the method outside the class, you don't mark it as virtual and you qualify its name:


void B::out(std::ofstrea& outFile)
{
outFile<< index << angle;
}


When defining your functions out of line you want to use something like this:


void B::out(std::ofstream& outFile)
{
outFile<< index << angle;
}

Any member of the respective class is accessible in a member function, independent of whether the function is virtual or not.


Since talking about functions reading or writing data: Unless you really need to make use of specific details of std::ofstream or std::ifstream, e.g., use open(), you should probably pass std::ostream or std::istream references. This way the functions can also be used with different stream stream types, e.g., std::ostringstream and std::istringstream.



Unless you have written your function body in the class declaration, you must prefix the member function name with the class name:


void B::out(std::ofstream& out)
{
out << index << angle;
}

What exact error did you get by the way, this would be much simpler if you could post the exact error code.



You are missing the class name before member function definition.


void B::out(std::ofstrea& outFile)  // Virtual keyword is optional in defintion.
{
// ...
}


When you define a class method in your cpp file you must give the class name as scope like this :


void B::out(std::ofstrea& outFile)
{
outFile<< index << angle;
}

Related Posts:

0 commentaires:

Enregistrer un commentaire