Copying DynMem and Custom Member Functions (a walk function)

Hi im new here. After studying php and pygtk for a few years I thought I would give c++ a go and ive been impressed with it so far. Im trying to write a basic tool class for myself just as a test (a List handling class similar to pythons tuple or list). I have been mostly successful with the basic operations on the list but there are a few things im unsure of.

Q1: Copying over Dynamic Memory with a new bit of Dynamic Memory. ???

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

class Test {
    // empty array of strings
    std::string *ch;
    Test () {
        ch = new std::string [5];
    }
    ~Test() {
        delete [] ch;
    }
    void reformat() {
        int new_size = 6;
        std::string *new_ch;
        new_ch = new std::string [new_size];
        ch = new_ch;
        
        // This line can produce error
        delete [] new_ch;

/* Without this delete it works. I don't think it should so
I must getting confused? If I take this out am I not leaving
a bit of memory somewhere undeleted, or is this covered in
the destructors delete [] ch because ive referenced by a pointer somehow? */

    }
};

int main () {

Test t;
t.reformat(); // t.ch will now be an array of 6 strings.

}


Q2: Creating an in class walk function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

class Test {
    int walk ( void (*functocall)() );
    void walking ();
};

// Trying to make a function to run on each element of an array.
int Test::walk ( void (*functocall)() ) {
    (*functocall)();
}

// Run this on each element of an array.
void Test::walking() {
}

int main () {

Test t;
// Ive had errors here. (Scope errors, Unary * errors and others.)
// Tried various changes like t->*walking , t.*walking, Test::walking etc.
// Im guessing im not getting something with how the custom functions & pointers work.
t.walk(t.walking);

}


Any help appreaciated with these issues. The second one is written with prototype style as thats closer to my proper class code and I have a feeling it might make a difference.
Q1: ch = new_ch; will make ch pointing to the same address of new_ch, so delete [] new_ch; will be the same as delete [] ch;

Q2: The pointers to member functions have quite a complicated syntax:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Test {
    public:
        int walk ( void (Test::*functocall)(), Test &obj );
        void walking ();
};

int Test::walk ( void (Test::*functocall)(), Test &obj ) {
    //if you want to call functocall from any object
    (obj.*functocall)();

    //if you want to call functocall from the owner of this function -in this case you don't need the second parameter-
    (this->*functocall)();
}


void Test::walking() {
}

int main () {

    Test t;
    t.walk(&Test::walking,t);

}



Excellent. Just what I was looking for. Thanks for the help.
Topic archived. No new replies allowed.