any type in a list

is there another way of doing this.
using codeblocks with valgrind plugin.
compiler is gcc.

valgrind report memory leak when return from a Any class return from function.
program also segmentation fault.

I would have posted all code but it way to big for this forum.

Any.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef ANY_HPP
#define ANY_HPP

class Any;

typedef unsigned int uint;
typedef unsigned long ulong;
typedef std::string str;
typedef std::vector<Any> list;

enum ObjType{
	objNONE,
	objERROR,
	objINT,
	objFLOAT,
	objLONG,
	objDOUBLE,
	objSTR,
	objLIST
};

union Variable {
    int* Int;
    float* Float;
    double* Double;
    long* Long;
    str* Str;
    list* List;
};

class Any {
    bool clear;
    bool nclear;
    ObjType type;
    Variable data;

    void Dump();
public:
    ~Any() { Clear(); }
    Any() { clear = true; nclear = true; type = objNONE; }
    Any(int v);
    Any(float v);
    Any(const char *v);
    Any(str v);
    Any(long v);
    Any(double v);
    Any(list v);
    Any(ObjType t);
    void operator() (ObjType t);
    void operator() (int v);
    void operator() (float v);
    void operator() (long v);
    void operator() (double v);
    void operator() (const char *v);
    void operator() (str v);
    void operator() (list v);

    //void operator= (Any v);
    Any operator[] (int pos);
    Any operator+ (Any v);
    Any operator- (Any v);
    Any operator* (Any v);
    Any operator/ (Any v);

    void Clear();
    void Append(Any v);
    //Any slice (int pos, int to, int step); // not done

    bool __int__();
    bool __float__();
    bool __double__();
    bool __long__();
    bool __string__();
    bool __list__();    // not done
    str __repr__();
    uint __len__();
};

#endif 
There's an article about this -> http://www.cplusplus.com/forum/articles/18756/

Or, if you're lazy, there's...

boost::any -> http://www.boost.org/doc/libs/1_46_1/doc/html/any.html and...
boost::variant -> http://www.boost.org/doc/libs/1_46_1/doc/html/variant.html
Strongly suggest looking at boost::any -- this is exactly what OP is sort of reinventing.
Looking for option with out boost.

just something i want to play with.
learning from my errors.
learning what create memory leaks.
understanding how pointers works.

My program works.

using my list command.
just lack undstanding why Any return from function create memory leak and segment fault.

if i don't use it in function then every works fine. valgrind finds no leaks.
Well it depends on the implementation. At a minimum, since the object contains pointers it manages, you'll need a proper copy constructor and assignment operator that do deep copies of the pointer. Your destructor (or perhaps your Clear() method) needs to deallocate the memory allocated by the object (I assume in all of your non-default constructors and your operator() methods).

The operator() method of giving a new value to the object is very strange, imo. Better would be to use assignment operators for that.
Topic archived. No new replies allowed.