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. ???
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.
}
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.
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);
}