Initializing a pointer in a class.

This is a discussion on Initializing a pointer in a class. within the C++ Programming forums, part of the General Programming Boards category; Im having a little trouble with a program i am writing. I have a class heirarchy: Project --> SimpleProject --> ...

  1. #1
    Registered User gpr1me's Avatar
    Join Date
    Mar 2006
    Posts
    14

    Initializing a pointer in a class.

    Im having a little trouble with a program i am writing. I have a class heirarchy:

    Project
    --> SimpleProject
    --> MinorProject
    ect...

    and another class:
    Task

    Now task has a pointer to a Project object in it's private section of the class definition.

    Here is Task.h

    Code:
    #ifndef TASK_H
    #define TASK_H
    
    // forward declaration
    class Project;
    
    class Task
    {
    
    public:
    	Task();	
    	Task(const Task & t);
    	Task(int, const Project &);	
    	~Task();
    
    	void DoIt();
    
    	Task & operator=(const Task & other);
    	friend std::ostream & operator<<(std::ostream & os,
    									 const Task & t);
    	friend bool operator==(const Task & left,
    						   const Task & right);
    	friend bool operator!=(const Task & left,
    						   const Task & right);
    
    private:
    	int id;
    	Project *pp;
    
    };
    
    #endif
    Now i want the Project *pp pointer to point to a derived class of Project (SimpleProject, MinorProject).

    I defined the constructors of Task like this (Task.cpp):

    Code:
    /**
    * Default Constructor
    */
    Task::Task()
    {
    	// do nothing
    }
    
    /**
    * Constructor with id and reference to project.
    */
    Task::Task(int in_id, const Project & in_pp)
    {
    	*pp = in_pp; // is this wrong?
    	id = in_id;
    }
    
    /**
    * Copy Constructor.
    */
    Task::Task(const Task & orig)
    {
    	id = orig.id;	
    	pp = orig.pp;
    
    }
    Of so the id is getting set but for some reason the pp pointer is not being set. It is always the same memory address for each pp pointer even if i create more than on instance of the class. Do i have to dynamically create the Project that pp is pointing to in the constructors? But if i do that then i will have to know what type of project it is.

    My main file is:

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    #include "Task.h"
    #include "SimpleProject.h"
    #include "MinorProject.h"
    #include "MajorProject.h"
    
    int main()
    {
    	SimpleProject *sp1 = new SimpleProject();
    	SimpleProject *sp2 = new SimpleProject(*sp1);
    	SimpleProject *sp3 = new SimpleProject(*sp1);
    	
    	Task *t1 = new Task(1, *sp1);
    	Task *t2 = new Task(2, *sp2);
    	Task *t3 = new Task(*t1);
    
    	sp1->AddTask(*t1);
    	sp2->AddTask(*t2);
    	sp3->AddTask(*t3);
    
    	cout << *sp1 << endl;
    	cout << *sp2 << endl;
    	cout << *sp3 << endl;
    
    	cout << (*sp1 == *sp2) << endl;
    
    	delete sp1;
    	delete sp2;
    	delete sp3;
    
    	return 0;
    }
    The only thing that is not working is the pp pointer in Task obejcts.
    Can anyone help me out?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    >> *pp = in_pp; // is this wrong?

    Yes. That copies the data in in_pp to whatever pp points to. What you want to do is assign the address of in_pp to pp. Of course, if pp is a pointer, you might as well make in_pp a pointer.

    You should initialize pp to 0 in the default constructor, or just makie pp a reference variable itself and get rid of the default constructor. You should also make more descriptive variable names than pp.

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Code:
    Task::Task(int in_id, const Project & in_pp)
    {
    	*pp = in_pp; // is this wrong?
    	id = in_id;
    }
    yes it is wrong
    *pp dereferences the pointer, but there is no object it's pointing at so nothing to dereference.
    You should just use: pp = &in_pp;
    to assign the pointer to the passed in object

  4. #4
    Registered User gpr1me's Avatar
    Join Date
    Mar 2006
    Posts
    14
    Thanks alot. I understand what i was doing wrong now.

    And yes, i was being lazy with my naming conventions. It's an awful habbit...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  4. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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