Newbie question......

D

Don

I am a newbie to CPP so have patience......
I have the small code piece below, I want to pass the object1, declared in
the main function, to the testfunc2 function as a pointer, making the
tester[8] of Object1 becoming 0x1A.

Does anyone know how this would be done......

Don


class mytestclass
{
public:
mytestclass(); // constructor
void testfunc();

private:
unsigned char tester[10]; //holds a list of announcements only 0 to 4 are
used
unsigned int mytest;
};


mytestclass::mytestclass()
{
tester[0] = 0x11;
tester[1] = 0x22;
tester[2] = 0x33;
tester[3] = 0x44;
tester[4] = 0x55;
tester[5] = 0x66;
mytest = 0;
}

void mytestclass::testfunc()
{
tester[8] = 0x1A;
}



void testerfunc2(mytestclass* testobj){
testobj.testfunc();
}


int main(){

mytestclass object1;

testerfunc2(&object1);

//printing object1.tester[8] should yield the result 0x1A

return 0;
}
 
A

Allan Bruce

Youre ideas are fine - see inline for comments

#include said:
class mytestclass
{
public:
mytestclass(); // constructor
void testfunc();

private:
unsigned char tester[10]; //holds a list of announcements only 0 to 4 are

unsigned int mytest;
};


mytestclass::mytestclass()
{
tester[0] = 0x11;
tester[1] = 0x22;
tester[2] = 0x33;
tester[3] = 0x44;
tester[4] = 0x55;
tester[5] = 0x66;
mytest = 0;
}

void mytestclass::testfunc()
{
tester[8] = 0x1A;
}



void testerfunc2(mytestclass* testobj){
testobj.testfunc();

// testobj is a pointer so use arrow instead
testobj->testfunc();
}


int main(){

mytestclass object1;

testerfunc2(&object1);

// printing out will cause an error because tester is private
// make an accessor (or temporarily make public for debug)
std::cout << object1.tester[8]
//printing object1.tester[8] should yield the result 0x1A

return 0;
}

The code you had was close, here is the full code that works - but note that
it is recommended to use an accessor function instead of making a variable
public. I leave that as homework for you to do.
Allan


// begin temp.cpp

#include <iostream>

class mytestclass
{
public:
mytestclass(); // constructor
void testfunc();

unsigned char tester[10]; //holds a list of announcements only 0 to 4 are
private:
unsigned int mytest;
};


mytestclass::mytestclass()
{
tester[0] = 0x11;
tester[1] = 0x22;
tester[2] = 0x33;
tester[3] = 0x44;
tester[4] = 0x55;
tester[5] = 0x66;
mytest = 0;
}

void mytestclass::testfunc()
{
tester[8] = 0x1A;
}

void testerfunc2(mytestclass* testobj)
{
testobj->testfunc();
}

int main()
{
mytestclass object1;

testerfunc2(&object1);

std::cout << object1.tester[8];

return 0;
}
 
A

Alexandros

I suggest the following:

....
void testerfunc2() // instead of: void
testerfunc2(mytestclass* testobj)
{
testfunc(); // which is the same as:
this->testfunc()
}
....

int main()
{
mytestclass object1;
object1.testerfunc2();
...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top