Thread: Accessing main form from functions in other classes

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    1

    Accessing main form from functions in other classes

    I'm trying to draw a line on a form through a function which is in a class that I created inside another .h and .cpp file. But I cannot give it access to the main form or it's controls. Is there any way that I can pass the form into the functions of the class or a way that I can access the main form or maybe just a picturebox on the form in which I can draw too. And if so, how can I declare a temporary form / picturebox inside the class which I can then assign to the form / picturebox of the main form to?

    PJ

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    This sounds like VB except for the references to .h and .cpp. In this instance you may want to make class A a friend of class B. I don't normally recommend this and normally using friend indicates to you that your class structure is seriously flawed, but there are certain times when a friend is the only way to get the job done. Usually those instances are when you want class A to manage all instances of class B, but you don't want the user to be able to explicitly instantiate class B. They must go through class A to instantiate class B. Of course then class A would need to be a singleton class to avoid more issues.

    But since A and B do not have any prior knowledge of each other and are not derived from each other this is sometimes the only way.

    Code:
    class B;
     
    class A
    {
    static friend class B;
    static vector<B> BList;
    public: 
    	A () {};
    	static unsigned int CreateB(void)
    	{
    	 B *temp=new B;
    	 BList.push_back(temp);
    	 ..set unique ID for new B
    	 return ID;
    	}
    	static B GetB(unsigned int _ID) {return BList[_ID];};
    private:
       //Hide copy constructor to prevent misuse and/or improper implicit instantiation 
       //Just in case somehow someone does get access - always assert as false
       //Fail in all cases
       A(A& obj) {ASSERT(false);};
    };
     
    class B
    {
      private:
    	 B() {}
    };
    I coded this while sitting here so I'm fairly sure it doesn't work as shown. But it shows the general principle where a friend is pretty much the only way to get the job done.
    Last edited by VirtualAce; 11-05-2004 at 09:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  2. Replies: 6
    Last Post: 05-15-2007, 10:47 PM
  3. Classes and member functions
    By uraliss in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2006, 07:38 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM