3 Replies - 318 Views - Last Post: 13 January 2013 - 03:03 PM Rate Topic: -----

#1 unknownmaster80  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 81
  • Joined: 06-August 11

How to create default constructor for pointer

Posted 13 January 2013 - 03:01 AM

Main class

void initCar(Queue&, Queue&);
Queue *q1, *q2; // Global variable

int main() {
   initCar(*q1, *q2);
}

void initCar(Queue &q1, Queue &q2) {
   Car c1 = Car("SJS1006Z","Toyota", "Saloon car");
	Car c2 = Car("SFW6666E", "hyundai", "Taxi (Saloon)");
	Car c3 = Car("SCF1006G","Mercedes", "Large Van");
	Car c4 = Car("SBQ1006Z", "Puma", "Saloon Car");

	q1.enqueue(c1);
	q2.enqueue(c1);
	q2.enqueue(c3);
	q1.enqueue(c4);
	q1.enqueue(c1);
	q1.enqueue(c1);
	q1.enqueue(c1);
	q2.enqueue(c2);
	q2.enqueue(c2);
}


Queue class

//Default constructor 
#include "Car.h"
#include <iostream>
#include <string>

using namespace std;

const int Q_MAX_SIZE = 20;
class Queue {
private:
	int size; // size of the queue
	Car carQueue[Q_MAX_SIZE];
	int front, rear;
public:
	Queue();
	~Queue();
};

Queue::Queue() { // default constructor
	size = 0;
	front = 0;
	rear = Q_MAX_SIZE -1;
}

// Constructor for pointer ???? 
Queue::Queue*() {
	size = 0;
	front = 0;
	rear = Q_MAX_SIZE -1;
}


but I got error. any hints for this? thanks!

This post has been edited by unknownmaster80: 13 January 2013 - 04:17 AM
Reason for edit:: Use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: How to create default constructor for pointer

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2886
  • View blog
  • Posts: 9,587
  • Joined: 12-December 12

Re: How to create default constructor for pointer

Posted 13 January 2013 - 06:00 AM

but I got error. any hints for this? thanks!


What errors? Where?
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: How to create default constructor for pointer

Posted 13 January 2013 - 06:51 AM

You don't use Queue::Queue*() { to create a pointer to an instance of a class you use a normal constructor. In your current code you would remove the following:

// Constructor for pointer ???? 
Queue::Queue*() {
	size = 0;
	front = 0;
	rear = Q_MAX_SIZE -1;
}

First stop using the global variables, define the variables in main(). You don't need the global variables in your current program anyway, because you are properly passing the variables to your function. You also don't need the pointers since you are passing the variables by reference to your function.

But if you insist on using pointers you will need to allocate memory for them using new/delete and change your initCar() function to take pointers instead of references.

You also need some way to access the private members of your class.

Jim
Was This Post Helpful? 1
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1048
  • View blog
  • Posts: 4,455
  • Joined: 09-June 09

Re: How to create default constructor for pointer

Posted 13 January 2013 - 03:03 PM

A pointer of any type is always the same size on the current system, 4-bytes on a 32-bit system. Declaring a pointer to a class, or any type, does not allocate memory for that class or type. It simply allocates enough memory to hold an address, which "points" to memory of that type.

You do not need to create a special constructor for pointer instances of any class or type, because the pointer does not allocate memory for that class or type.

i.e.
#include <iostream>

class Foo {
   int a, b;
public:
   Foo() 
      :a(0), b(0) {}
};

int main() {
   Foo *ptr;
   Foo f;

   std::cout<<"Size of ptr = "<<sizeof(ptr)<<std::endl;
   std::cout<<"Size of f = "<<sizeof(f)<<std::endl;

   //now assign ptr to address of f
   ptr = &f;

   //The output is the same as above
   std::cout<<"Size of ptr = "<<sizeof(ptr)<<std::endl;
   std::cout<<"Size of f = "<<sizeof(f)<<std::endl;

   return 0;
}



Remember that pointers do not allocate memory for there targets.

This post has been edited by jjl: 13 January 2013 - 03:05 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1