Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

c++ is there a way of use properties allready declared in the header file inside implementation

I know it’s a basic question, but I didn’t find the answer anywhere.

Supose we have this header:

#pragma once;
#include "user.h"

class Teacher
{
public:
    float teachSkill = 0.01;
    void teach(User &user);
};

And a implementation like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include "teacher.h"

class Teacher
{
public:
    float teachSkill;
    void teach(User &user)
    {
        user.knowledge += (*this).teachSkill;
    }
};

if we already declared that teachSkill property in the header, is there a way c++ compiler can understand that this property is in the header on an implementation like this:

#include "teacher.h"

class Teacher
{
public:
    void teach(User &user)
    {
        user.knowledge += (*this).teachSkill;
    }
};

>Solution :

You can simply write, in the implementation:

void Teacher::teach(User &user)
{
    user.knowledge += (*this).teachSkill;
}

No need to re-declare the class there.

Furthermore, you don’t need (*this), so it’s simply:

void Teacher::teach(User &user)
{
    user.knowledge += teachSkill;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading