简体   繁体   中英

Can I initialize a class member variable using function in C++? If so how to do it?

I tried to initialize Structure class's member variables using function from StructureMaker class's function. It gives error. What is the problem with it? Can't I do so? Then how to do it?

MyMemberNotToInitThroughConstructor.h

#ifndef MYMEMBERNOTTOINITTHROUGHCONSTRUCTOR_H_
#define MYMEMBERNOTTOINITTHROUGHCONSTRUCTOR_H_

namespace my {

class MyMemberNotToInitThroughConstructor {
public:
    MyMemberNotToInitThroughConstructor(int no1);
    virtual ~MyMemberNotToInitThroughConstructor();

//private:
    int no;
};

} /* namespace my */

#endif /* MYMEMBERNOTTOINITTHROUGHCONSTRUCTOR_H_ */

MyMemberNotToInitThroughConstructor.cpp

#include "MyMemberNotToInitThroughConstructor.h"

namespace my {

MyMemberNotToInitThroughConstructor::MyMemberNotToInitThroughConstructor(int no1) {
    no = no1;
}

MyMemberNotToInitThroughConstructor::~MyMemberNotToInitThroughConstructor() {
    // TODO Auto-generated destructor stub
}

} /* namespace my */

Structure.h

#ifndef STRUCTURE_H_
#define STRUCTURE_H_

#import "MyMemberNotToInitThroughConstructor.h"

namespace my {

class Structure {
public:
    Structure();
    void setElements1(int id1);
    void setElements2(MyMemberNotToInitThroughConstructor* member1);
    virtual ~Structure();

//private:
    int id;
    MyMemberNotToInitThroughConstructor*member;
};

} /* namespace my */

#endif /* STRUCTURE_H_ */

Structure.cpp

#include "Structure.h"

namespace my {

Structure::Structure() {
}

void Structure::setElements1(int id1)
{
    id = id1;
}
void Structure::setElements2(MyMemberNotToInitThroughConstructor* member1)
{
    member = member1;
}

Structure::~Structure() {
    // TODO Auto-generated destructor stub
}

} /* namespace my */

StructureMaker.h

#ifndef STRUCTUREMAKER_H_
#define STRUCTUREMAKER_H_

#import "Structure.h";
#import "MyMemberNotToInitThroughConstructor.h"

namespace my {

class StructureMaker {
public:
    StructureMaker();
    void makeStructure(Structure*st);
    void innerMake(Structure*st);
    virtual ~StructureMaker();
};

} /* namespace my */

#endif /* STRUCTUREMAKER_H_ */

StructureMaker.cpp

#include "StructureMaker.h"

namespace my {

StructureMaker::StructureMaker() {
    // TODO Auto-generated constructor stub

}

void StructureMaker::makeStructure(Structure*st)
{
    int id1 = 123;
    st->setElements1(id1);
    innerMake(st);
}

void StructureMaker::innerMake(Structure*st)
{
    int no1 = 987;
    MyMemberNotToInitThroughConstructor my(no1);
    st->setElements2(&my);
}

StructureMaker::~StructureMaker() {
    // TODO Auto-generated destructor stub
}

} /* namespace my */

Test.cc

#include <stdio.h>
#include<iostream>
#include"StructureMaker.h"
#include"Structure.h"

using namespace std;
using namespace my;

int main()
{
    StructureMaker stm;
    Structure*st;
    stm.makeStructure(st);
    //cout << st->member->no << endl;
    return 0;
}

The problem lies in the following function:

void StructureMaker::innerMake(Structure*st)
{
    int no1 = 987;
    MyMemberNotToInitThroughConstructor my(no1);
    st->setElements2(&my);
}

my is a local variable in the function. You are setting a member variable of st to be a pointer to that local variable. Once that function returns, the pointer becomes invalid, If you try to use that pointer to access its member variable or use it to call a member function, you will run into undefined behavior territory.

It will be better to allocate an object from heap and use it.

void StructureMaker::innerMake(Structure*st)
{
    int no1 = 987;
    st->setElements2(new MyMemberNotToInitThroughConstructor(no1));
}

Remember that Structure has not taken over the responsibility of managing that pointer. You'll have to make sure that you deallocate the memory somewhere in Structure . For such a class, you'll need to make sure that you have a copy constructor and a copy assignment operator that do the right thing.

Suggested absolutely must reading: The Rule of Three

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM