"passing a class object as a parameter to another class" questions

Having some issues trying to figure out when and where to use pointers, addresses of, pass-by-reference, pass-by-value, etc...

I have two classes. TestClass1 and TestClass2.

TestClass2 can stand on its own, or it can occasionally take an object of TestClass1 as a parameter. When it does so, one of its methods utilizes a method from TestClass1. Therefore, TestClass2 has an overloaded constructor and an overloaded class method.

I'm confused as to how to pass an object of TestClass1 to TestClass2, and how to utilize the TestClass1 method within a TestClass2 method. I.E. when do I use *, and/or &

Hopefully, my pseudo code is understandable.

TestClass1.h

#ifndef TEST_CLASS_1_INCLUDED
#define TEST_CLASS_1_INCLUDED

#include <TestClass1.h>

class TestClass1
{
	public:
	
		TestClass1(uint8_t class1param1, uint8_t class1param2)
                 : m_class1param1(class1param1), m_class1param2(class1param2){}
			
		void begin();
		
		void Class1Func();
				
	private:
	
		uint8_t m_class1param1;
		uint8_t m_class1param2;
                uint8_t class1val;
};

TestClass1.cpp

#include "TestClass1.h"

void TestClass1::begin()
{
	StandardFunction(m_class1param1);
}

void TestClass1::Class1Func()
{
	class1val = anotherStandardFunction(m_class1param2);
	return class1val;
};

TestClass2.h

#ifndef TEST_CLASS_2_INCLUDED
#define TEST_CLASS_2_INCLUDED

#include <TestClass2.h>
#include <TestClass1.h>

class TestClass2
{
	public:
	
		TestClass2(uint8_t class2param1, uint8_t class2param2)
                 : m_class2param1(class2param1), m_class2param2(class2param2){}
			
		void begin();
		
		void Class2Func();
		
	private:
	
		uint8_t m_class2param1;
		uint8_t m_class2param2;
                uint8_t class2val;
};

TestClass2.cpp

#include "TestClass2.h"

void TestClass2::begin()
{
	aStandardFunction(m_class2param1);
}

void TestClass2::begin(TestClass1_instance)
{
	TestClass1_instance.aStandardFunction(m_class2param1);
}

void TestClass2::Class2Func()
{
	class2val = anotherStandardFunction(m_class2param2);
	return class2val;
};
		
void TestClass2::Class2Func(TestClass1_instance)
{
	uint8_t tempVal = TestClass1_instance.anotherStandardFunction(m_class2param2);
        class2val = anotherStandardFunction(tempVal);
	return class2val;
};

Resulting possible .ino files:

TestCode1.ino

#include TestClass1.h
#include TestClass2.h

TestClass1 obj1(SomeParam1, SomeParam2);
TestClass2 obj2(AnotherParam1, AnotherParam2);

void setup()
{
   obj1.begin();
   obj2.begin();
}

void loop()
{
   uint8_t Results1 = obj1.Class1Func();
   uint8_t Results2 = obj2.Class2Func();
   /* 
    * In this case, obj1 and obj2 are unrelated and the overloaded
    * obj2 constructor and method are not called
    */

TestCode2.ino

#include TestClass1.h
#include TestClass2.h

TestClass1 obj1(SomeParam1, SomeParam2);
TestClass2 obj2(obj1, AnotherParam1, AnotherParam2);

void setup()
{
   obj1.begin();
   obj2.begin();
}

void loop()
{
   uint8_t Results1 = obj1.Class1Func();
   uint8_t Results2 = obj2.Class2Func();
   /* 
    * In this case, obj1 and obj2 are related and the overloaded
    * obj2 constructor and method are called
    */

Any help would be greatly appreciated,
Thanks.

Replying to my own question to bump it to the top of the list...any help would be greatly appreciated.

Thanks,
Dennis

I don't think you can use a Reference data member as calling the empty constructor would leave it uninitialized. So, I'd use a pointer data member. Something like:

class class1 {
  public:
    void class1MemberFunction() {
      Serial.println("Class1 Function Called");
    }
};

class class2 {
  public:
    class2() {
    }

    class2(class1 &class1Ref): ptr(&class1Ref) {
    }

    void testFunction() {
      if (ptr != nullptr) {
        ptr->class1MemberFunction();
      } else {
        Serial.println("Class1 Function Not Called");
      }
    }
    
  private:
    class1 *ptr = nullptr;
};

class1 object1;
class2 object2a;
class2 object2b(object1);

void setup() {
  Serial.begin(115200);
  delay(1000);
  object2a.testFunction();   // this will not call the function in class1
  object2b.testFunction();   // this will call the function in class1
}

void loop() {
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.