Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


greenzone

Member Since 24 Jun 2011
Offline Last Active Jun 04 2014 07:57 AM

Topics I've Started

private: instance in class with in namespace Question

26 April 2014 - 12:41 PM

I am having a problem with the use of a class member that does not seem to be constructed.

 

I have a name space called "DogActivity".

 

In this name space there are two classes " DogSounds and Bark".

 

In the Bark class I have a private: instance of DogSounds.

 

Now in the constructor of the DogSounds class there is a private: int variable called NumSounds that is initialized to 0.

 

 

 

The problem is if i create an instance of DogSound in my main program the constructor is called and the NumSounds variable is set to 0 as planned.

 

But

 

If i create an instance of Bark in the main program the constructor of DogSounds is never called and NumSounds is never initialized.

 

here is an example of my namespace with classes.

namespace DogActivity
{

    class DogSounds
    {
            public:
             DogSounds() { NumSonds = 0;}

            private:
               int NumSounds;
     };

     class Bark{

       private:
            DogSounds dogSounds;

     };

}

Now here is how i would use in main



using namespace DogActivity;

int main ( int argc, char * argv[] ) 
{

Bark bark;

// Do work with bark bellow

}

But when I create the instance of Bark the privat: dogSounds' variable "NumSounds" is never set to 0. The Constructor of dogSounds is never called, why is this? shouldn't the constructor of dogSounds be called as soon as i create an instance of Bark? what might i be doing wrong here?


operator overloading member in namespace question

02 April 2014 - 06:02 PM

Ok so I have scoured the deepest recesses of the internet to find an example of how to define the implementation of a member function that is overloading an operator for a class that is with in a namespace.

 

interface looks like this

namespace A
{

  class dog
  {
   public:
   dog();

    bool operator == ( const dog & other ) const;

   };

private:
float hieght;
float weight;
}
  


If I define the implementation with in the header it would look like this which works. but i want it in the Cpp file

namespace A
{

  class dog
  {
   public:
   dog();

    bool operator == ( const dog & other ) const
    {
     return weight == other.weight && hieght == other.hieght;
    }

   };

private:
float hieght;
float weight;
}
  


If I wanted to separate the implementation from the header to the cpp file how would I write the implementation of this overloaded function? 

 

just to show how i am writing the other member function's emplimetation in the cpp file here is an example.

bool A::dog::bark() {}

Thanks

 


Qestion about passing incremented values

06 December 2013 - 08:49 AM

Ok, so I completely understand the recursive portion of the Merge Sort algorithm but what I am having problems with is this example of the actual merge portion of the algorithm. In the first conditional statement were one is comparing the first element of the two input vectors I understand, but its the next line were p1 or p2 are being incremented up 1, while specifying which element is to be moved on to the empty vector, I am not understanding. Is it adding element 0 and then ++ to element 1 for the next comparison or is it passing element 1?

 

I guess my main question is, does the value of a variable get read first and then incremented or is the variables' value incremented and then read?

 

int newVariable = 0, oldVariableX = 0;

 

newVariable = oldVariableX++

 

Does  ( newVariable = 0   or   newVarivable = 1  ) ?

void Merge(Vector<int> & vec, Vector<int> & v1, Vector<int> & v2) 
{
     int n1 = v1.size();
     int n2 = v2.size();
     int p1 = 0;
     int p2 = 0;
     while (p1 < n1 && p2 < n2) {
         if (v1[p1] < v2[p2]) {
          vec.add(v1[p1++]);
         } else {
          vec.add(v2[p2++]);
         }
     }
    while (p1 < n1) vec.add(v1[p1++]);
    while (p2 < n2) vec.add(v2[p2++]);
}

placing constructor in private

24 November 2013 - 02:06 PM

I am currently studying the design patterns described by the Gang of Four. The Composite Pattern, is what I am currently trying to concertize mentally. While doing this I was looking at an example of its implementation shown here. If you notice in the portion were they are implementing the leaf classes of this pattern they place a generic version of the classes" constructor in private and they place an overloaded version of the constructor in public. Trying to figure this out on my own I came across a forum post that describes the various reasons one might place the constructor in private here by Kerido. Of those 5 examples I am certain its not B or E. But out of A,C, and D I am not sure. I place portions of the code I am talking about to reduce having to hunt through the posted link above. Also the explanations of  A, C, and D (reasons to have a constructor in private) I'll past below the class in question.

/*
 * Capacitor is a LEAF class which is directly inherited from the -
 * ElectronicComponet class.
 */
class Capacitor:public ElectronicComponent
{
	float capacitance_muF; //Capacitance value in micro fared.

	Capacitor(){} // Point in question. Why are they doing this, in this context?
public:
	/*
	 * Constructor - initialize the capacitance value.
	 */
	Capacitor(float muF)
	:capacitance_muF(muF)
	{

	}
	/*
	 * Prints the Capacitor's specification.
	 */
	void PrintSpec(ostream& ostr, string prefix = "")
	{
		ostr<<prefix<<"Capacitor ("<< capacitance_muF <<" muF )"<<endl;
	}
	/*
	 * Performs the Capacitor's real function of modifying  input Voltage & Current.
	 * Electrical engineers, please help :)
	 * Now I just put a printing of Voltage and current as place holder.
	 */
	void DoFunction(float& voltage, float& current)
	{
		cout<<endl<<"Capacitor Input ("<<voltage<<" V ,"<<current<<" Amp) 
			Capacitance ="<< capacitance_muF<<endl;
	}
};

A. Your class instances are created in a static method. The static method is then declared as public.

class MyClass()
{
private:
  MyClass() { }

public:
  static MyClass * CreateInstance() { return new MyClass(); }
};

C. (Only applies to the upcoming C++0x standard) You have several constructors. Some of them are declared public, others private. For reducing code size, public constructors 'call' private constructors which in turn do all the work. Your public constructors are thus called delegating constructors:

class MyClass
{
public:
  MyClass() : MyClass(2010, 1, 1) { }

private:
  MyClass(int theYear, int theMonth, int theDay) { /* do real work */ }
};

D. You want to limit object copying (for example, because of using a shared resource):

class MyClass
{
  SharedResource * myResource;

private:
  MyClass(const MyClass & theOriginal) { }
};

Any thoughts on the matter would be nice thanks

 

 


Most efficient Picking Method

08 November 2013 - 08:54 AM

I am curious as to what is the most efficient picking method. Currently I render all the objects of the screen assigning each a different color and then what ever color is under the mouse during picking is the object that is then selected. I have also heard if i change the size of the screen during picking to be just a little larger then the mouse and then drawing the screen right under the mouse during the picking pass helps with the efficiency as well. Also is casting a ray more efficient then doing a render pass for handling picking?  Any thoughts on this would be pure awesomeness thanks biggrin.png


PARTNERS