problems with retrieving the value of a class pointer object member allocated with operator new inside a function

Leonardo Miquelutti

I'm having problem with the following code. I try to fill in a member of an object called station inside a function, but I cannot retrieve it in main().

This is my header foo.h

class DirectoryProperties
{
public:
    size_t      numberOfFolders;

    void        initialize_stations( StationBase *station );

private:
    void        fill_station_names( StationBase *station );
};

class StationInfo
{
public:
    std::string     name;
};

And this is my foo.cpp

#include "foo.h"

void DirectoryProperties::fill_station_names( StationBase *station )
{
    station[0].name = "dummy";
}

void DirectoryProperties::initialize_stations( StationBase *station )
{
    size_t N = 1;

    station = new StationBase[ N ];

    this->fill_station_names( station );

    // this works
    std::cout << station[0].stationName << std::endl;
}

int main()
{
    DirectoryProperties dirInfo;

    StationBase *station = NULL;

    dirInfo.initialize_stations( station );

    // breaks here
    std::cout << station[0].stationName << std::endl;

    return 0;
}

So, I can correcly print station[0].name inside DirectoryProperties::initialize_stations( StationBase *station ), but no in main().

It also breaks if I try

int main()
{
    DirectoryProperties dirInfo;

    StationBase *station = NULL;

    dirInfo.initialize_stations( station );

    // breaks here
    station[0].stationName = "dummy";

    return 0;
}

So I assume that object pointer station has no memory allocated in main.

juanchopanza

station is a local variable in initialize_stations, so no changes to it have an effect outside of the function.

An immediate fix is to have the function return a pointer to the newed array.

StationBase* DirectoryProperties::initialize_stations(  )
{
  ....
  StationBase* station = new StationBase[ N ];
  ....
  return station;
}

A better solution is to return an std::vector<StationBase>. Or just a single StationBase, since you're only allocating one object anyway.

std::vector<stationBase> DirectoryProperties::initialize_stations()
{
    size_t N = 1;
    std::vector<stationBase> station(N);
    fill_station_names( station );
    return station;
}

and fix fill_station_names accordingly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does taking a member function pointer value requires class name qualification even from inside of the class?

From Dev

The issues of member object alignment of a class with virtual function and dynamically allocated

From Dev

Trying to use * pointer operator to call class member function

From Dev

Void pointer to a class object: Initialization inside a function

From Dev

pointer to a member function of a class

From Dev

Pointer to class member function

From Dev

Address operator with pointer to member function

From Dev

How to call pointer member function inside a class definition?

From Dev

Using memory allocated pointer inside the function

From Dev

Function pointer to class member function

From Dev

pointer to member function inside namespace

From Dev

Function pointer to class template member

From Dev

Return a pointer to class member function

From Dev

Member function pointer of template class

From Dev

Member function pointer on derived class

From Dev

pointer member of a class has to point to heap-allocated data?

From Dev

overloading the + operator in a class with a pointer member variable

From Dev

Where the function pointer is allocated when its begin executed from an class object?

From Java

Why defining buffer length of a buffer leads to that class's function member to loose the value of a function pointer member variable?

From Dev

C++ Function call via an object with public member pointer to function, without using dereference operator

From Dev

C++ External function with pointer to function as parameter, used inside a class with a member function

From Dev

cast a pointer to member function in derived class to a pointer to abstract member function

From Dev

cast a pointer to member function in derived class to a pointer to abstract member function

From Dev

Class member access inside the member function body

From Dev

Return a function pointer from a class member function

From Dev

C++ Initialize object member of a struct that is allocated using the "new" keyword

From Dev

Assign function pointer inside class

From Dev

C++ pointer to object's class member

From Dev

PHP Define a new PDO object inside class (outside function)

Related Related

  1. 1

    Why does taking a member function pointer value requires class name qualification even from inside of the class?

  2. 2

    The issues of member object alignment of a class with virtual function and dynamically allocated

  3. 3

    Trying to use * pointer operator to call class member function

  4. 4

    Void pointer to a class object: Initialization inside a function

  5. 5

    pointer to a member function of a class

  6. 6

    Pointer to class member function

  7. 7

    Address operator with pointer to member function

  8. 8

    How to call pointer member function inside a class definition?

  9. 9

    Using memory allocated pointer inside the function

  10. 10

    Function pointer to class member function

  11. 11

    pointer to member function inside namespace

  12. 12

    Function pointer to class template member

  13. 13

    Return a pointer to class member function

  14. 14

    Member function pointer of template class

  15. 15

    Member function pointer on derived class

  16. 16

    pointer member of a class has to point to heap-allocated data?

  17. 17

    overloading the + operator in a class with a pointer member variable

  18. 18

    Where the function pointer is allocated when its begin executed from an class object?

  19. 19

    Why defining buffer length of a buffer leads to that class's function member to loose the value of a function pointer member variable?

  20. 20

    C++ Function call via an object with public member pointer to function, without using dereference operator

  21. 21

    C++ External function with pointer to function as parameter, used inside a class with a member function

  22. 22

    cast a pointer to member function in derived class to a pointer to abstract member function

  23. 23

    cast a pointer to member function in derived class to a pointer to abstract member function

  24. 24

    Class member access inside the member function body

  25. 25

    Return a function pointer from a class member function

  26. 26

    C++ Initialize object member of a struct that is allocated using the "new" keyword

  27. 27

    Assign function pointer inside class

  28. 28

    C++ pointer to object's class member

  29. 29

    PHP Define a new PDO object inside class (outside function)

HotTag

Archive