pointer of array of class

This is a discussion on pointer of array of class within the C++ Programming forums, part of the General Programming Boards category; hello! i have problem with program i made: the program should define a pointer to pointer which points to dynamic ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    pointer of array of class

    hello!

    i have problem with program i made:
    the program should define a pointer to pointer which points to dynamic array of Grade (class i made).
    i increase the size of the array in function. so i made a new pointer to pointer which point to array of Grade (the class i made). i transferred the old array to new one, and finaly, before i going out of the function, i shoud transfer it's address to the first pointer i made:
    Code:
    {
    int students=1;
    	
    	Grade ** numGrades=new Grade * [students];
    	numGrades[0]=new Grade;
    	assert(numGrades!=0);
    	AddGrade(numGrades,students);	
    }
    void AddGrade(Grade ** _numGrades,int& _students)
    {
    	_students++;
    	Grade ** gradeTemp=new Grade * [_students];
    	for(int i=0;i<_students-1;i++)
    		gradeTemp[i]=_numGrades[i];
    	gradeTemp[i]=new Grade;
    
    }
    how can i transfer the address of "gradeTemp" to "numGrades"?

    thank you,
    Moshiko

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    The same way you did for _students, use a reference.

    BTW, symbols which begin with an underscore are reserved identifiers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User scarfish's Avatar
    Join Date
    Apr 2008
    Posts
    3
    or just call AddGrade with the adress of numGrades and use it in AddGrade as a pointer of a pointer of a pointer^^

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    Code:
    	AddGrade(numGrades,students);	
    
    void AddGrade(Grade ** &_numGrades,int& _students)
    {
    .
    .
    .
    }
    but i can't define it like this, it write an error. so how can i define _numGrade as a reference of numGrades?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Well you could start with posting your error message.

    Did you also fix the prototype to reflect that change?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    I would rather call it pass by pointer than pass by reference. Pass by reference is what we do in C++.
    A nitpick detail, I know, but it's more correct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21