Thread: Using Friend Class

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    52

    Using Friend Class

    /*
    I'm trying to develope a fried class (c2) capable of accessing
    class cl for the purpose of gathering data. I want to pass a
    cl pointer to c2::runShow. runShow would subsequently pass the
    pointer to other c2 member functions. The code compiles without
    error or warnings. It reads the first object of the array of
    objects, but when c2::prn increments i, the following error
    message is displayed:

    Loaded 'C:\WINDOWS\SYSTEM\KERNEL32.DLL', no matching symbolic
    information found. First-chance exception in testCPP.exe:
    0xC0000005: Access Violation.

    I know it can't locate the subsequent objects of the array of
    objects and therefore has an access violation. I can't figure
    out why.

    If you could point out my blounder, I'd be happy.

    Thanks
    */

    #include <iostream>

    class cl {
    int a, b, c;
    static int cntr;
    public:
    cl( int j, int k, int l )
    { a = j; b = k; c = l; cntr++;
    std::cout << "Object " << cntr << '\n'; }
    ~cl() { std::cout << "Object " << cntr-- << " destroyed.\n"; }
    int get_a () { return a; }
    int get_b () { return b; }
    int get_c () { return c; }

    friend class c2;

    };


    class c2 {
    public:
    void prn( cl **obj )
    {
    std::cout << "Using class cl access function\n\n";
    for( int i = 0; i < 3; i++ ) {
    std::cout << obj[i]->get_a() << ", ";
    std::cout << obj[i]->get_b() << ", ";
    std::cout << obj[i]->get_c() << ",\t";
    } // end for

    std::cout << "\n\n";
    } // end prn

    void runShow( cl *ob )
    {
    prn( &ob );
    } // end show
    }; // end c2

    int cl::cntr = 0; // initialize counter


    int main() {

    cl ob[3] = { cl(1, 4, 7), // create array of objects
    cl(2, 5, 8),
    cl(3, 6, 9) }; // initializer
    c2 m; // create friend object

    m.runShow( ob );

    return 0;
    } // end

  2. #2
    Unregistered
    Guest
    Below are the changes I made.

    Code:
    #include <iostream> 
    
    class cl 
    { 
    	int a, b, c; 
    	static int cntr; 
    public: 
    	cl( int j, int k, int l ) 
    	{ 
    		a = j; b = k; c = l; cntr++; 
    		std::cout << "Object " << cntr << '\n'; 
    	}
    	
    	~cl() { std::cout << "Object " << cntr-- << " destroyed.\n"; } 
    
    	int get_a () { return a; } 
    
    	int get_b () { return b; } 
    
    	int get_c () { return c; } 
    
    friend class c2; 
    
    }; 
    
    
    class c2 
    { 
    public: 
    
    	void prn( cl **obj ) 
    	{ 
    		std::cout << "Using class cl access function\n\n"; 
    		for( int i = 0; i < 3; i++ ) { 
    			std::cout << obj[i]->get_a() << ", "; 
    			std::cout << obj[i]->get_b() << ", "; 
    			std::cout << obj[i]->get_c() << ",\t"; 
    		} // end for 
    
    	std::cout << "\n\n"; 
    	} // end prn 
    
    	void runShow( cl **ob ) //added * so it would take pointer to pointers
    	{ 
    		prn( ob ); //remove & reference 
    	} // end show 
    }; // end c2 
    
    int cl::cntr = 0; // initialize counter 
    
    
    int main() { 
    
    cl* ob[3] = { //added * for array of pointers of cl
    	&cl(1, 4, 7), // added reference to take address of
    	&cl(2, 5, 8), 
    	&cl(3, 6, 9) 
    			}; // initializer 
    
    	c2 m; // create friend object 
    
    	m.runShow( ob ); 
    
    	return 0; 
    } // end

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    52
    Thank you for your help.

    Could you explain why the array of pointers to objects works better than the array of objects?

    Also why does it immediatly destroy the object after it is created?

    Object 1 created
    Object 1 destroyed.
    Object 1 created
    Object 1 destroyed.
    Object 1 created
    Object 1 destroyed.
    Using class cl access function

    1, 4, 7, 2, 5, 8, 3, 6, 9,


    Press any key to continue

    It still gives me the correct data and it can be manipulated. Any ideas?
    Last edited by Ted; 10-14-2001 at 09:19 PM.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    I don't know why when I was here last time it didn't use my name it just called me Unregistered, anyway.

    If you change main to below it will give the objects their desired life expectancies.

    The reason the objects were destroy before the creation of the next object is because they were just temporary and had no name to associate with allocated space so they were deleted.

    &cl(1, 4, 7),// note there is no name just address of a cl.
    &cl(2, 5, 8),
    &cl(3, 6, 9)

    The reason the objects could still be displayed is that they were allocated on the stack and while they had been destroyed the location they were at had not been reallocated for something else so the data was availible. I'm glad you caught that, I didn't see it. Using invalid memory is bad ju-ju and should not be done unless you enjoy the trap handler throwing exceptions at you.

    Code:
    int main() 
    { 
    	cl* ob[3];
    
    	cl a(1, 4, 7);
    	cl b(2, 5, 8); 
    	cl c(3, 6, 9); 
    
    	ob[0] = &a;
    	ob[1] = &b;
    	ob[2] = &c;
    
    	c2 m; // create friend object 
    
    	m.runShow( ob ); 
    
    	return 0; 
    } // end

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 39
    Last Post: 01-18-2009, 11:53 PM
  2. Problem with friend class declaration in a namespace
    By Angus in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 01:29 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM