Results 1 to 6 of 6

Thread: Quick C++ question

  1. #1
    Senior Member
    Join Date
    Aug 2004
    Location
    W Yorkshire
    Posts
    5,691
    Thanks
    85
    Thanked
    15 times in 13 posts
    • XA04's system
      • Motherboard:
      • MSI X570-A Pro
      • CPU:
      • AMD Ryzen 5 3600
      • Memory:
      • Corsair 2x 8gb DDR 4 3200
      • Storage:
      • 1TB Serpent M.2 SSD & 4TB HDD
      • Graphics card(s):
      • Palit RTX 2060
      • PSU:
      • Antec Truepower 650W
      • Case:
      • Fractcal Meshify C
      • Operating System:
      • Windows 10
      • Monitor(s):
      • iiyama 34" Curved UWQHD
      • Internet:
      • Virgin 100mb Fibre

    Quick C++ question

    Code:
    template <class T>
    class Calculator
    {
    public:
    	Calculator(const T &x, const T &y) : _x(x), _y(y) {}
    	~Calculator(void){ }
    	const T add(void){ return _x + _y; }
    	const T sub(void){ return _x -_y; }
    	const T mult(void){ return _x * _y; }
    	const T div(void){ return _x / _y; }
    
    private:
    	const T _x;
    	const T _y;
    };
    The part in bold above. What does the colon mean/do? What's it called?! It's came out of nowhere in my work at uni and in my book. If someone can tell me what it's called then that would be great and I can find out from there.

    Cheers!

  2. #2
    Senior Member
    Join Date
    Aug 2004
    Location
    W Yorkshire
    Posts
    5,691
    Thanks
    85
    Thanked
    15 times in 13 posts
    • XA04's system
      • Motherboard:
      • MSI X570-A Pro
      • CPU:
      • AMD Ryzen 5 3600
      • Memory:
      • Corsair 2x 8gb DDR 4 3200
      • Storage:
      • 1TB Serpent M.2 SSD & 4TB HDD
      • Graphics card(s):
      • Palit RTX 2060
      • PSU:
      • Antec Truepower 650W
      • Case:
      • Fractcal Meshify C
      • Operating System:
      • Windows 10
      • Monitor(s):
      • iiyama 34" Curved UWQHD
      • Internet:
      • Virgin 100mb Fibre

    Re: Quick C++ question

    Nobody?

  3. #3
    Senior Member oolon's Avatar
    Join Date
    Mar 2007
    Location
    London
    Posts
    2,294
    Thanks
    150
    Thanked
    302 times in 248 posts
    • oolon's system
      • Motherboard:
      • Asus P6T6
      • CPU:
      • Xeon w3680
      • Memory:
      • 3*4GB Kingston ECC
      • Storage:
      • 160GB Intel G2 SSD
      • Graphics card(s):
      • XFX HD6970 2GB
      • PSU:
      • Corsair HX850
      • Case:
      • Antec P183
      • Operating System:
      • Windows 7 Ultimate and Centos 5
      • Monitor(s):
      • Dell 2408WFP
      • Internet:
      • Be* Unlimied 6 down/1.2 up

    Re: Quick C++ question

    Quote Originally Posted by XA04 View Post
    Nobody?
    Constructors.... inhertance? the constructors for your template class is calling the constructors for _x and _y classes with arguments passed to it.... Of course the whole line is inlined because of the {} in the definition.

  4. #4
    Senior Member
    Join Date
    Aug 2004
    Location
    W Yorkshire
    Posts
    5,691
    Thanks
    85
    Thanked
    15 times in 13 posts
    • XA04's system
      • Motherboard:
      • MSI X570-A Pro
      • CPU:
      • AMD Ryzen 5 3600
      • Memory:
      • Corsair 2x 8gb DDR 4 3200
      • Storage:
      • 1TB Serpent M.2 SSD & 4TB HDD
      • Graphics card(s):
      • Palit RTX 2060
      • PSU:
      • Antec Truepower 650W
      • Case:
      • Fractcal Meshify C
      • Operating System:
      • Windows 10
      • Monitor(s):
      • iiyama 34" Curved UWQHD
      • Internet:
      • Virgin 100mb Fibre

    Re: Quick C++ question

    Quote Originally Posted by oolon View Post
    Constructors.... inhertance? the constructors for your template class is calling the constructors for _x and _y classes with arguments passed to it.... Of course the whole line is inlined because of the {} in the definition.
    The thing I don't understand is that there aren't any _x and _y classes? Or is that actually defining those classes right there and then? If so that makes sense!

  5. #5
    Senior Member oolon's Avatar
    Join Date
    Mar 2007
    Location
    London
    Posts
    2,294
    Thanks
    150
    Thanked
    302 times in 248 posts
    • oolon's system
      • Motherboard:
      • Asus P6T6
      • CPU:
      • Xeon w3680
      • Memory:
      • 3*4GB Kingston ECC
      • Storage:
      • 160GB Intel G2 SSD
      • Graphics card(s):
      • XFX HD6970 2GB
      • PSU:
      • Corsair HX850
      • Case:
      • Antec P183
      • Operating System:
      • Windows 7 Ultimate and Centos 5
      • Monitor(s):
      • Dell 2408WFP
      • Internet:
      • Be* Unlimied 6 down/1.2 up

    Re: Quick C++ question

    Here it is at work....

    Code:
    #include <iostream>
    
    class blob
    {
    public:
      blob(const char *str){std::cout << str;}
    };
    
    template <class T>
    class Calculator
    {
    public:
            Calculator(const T &x, const T &y) : _x(x), _y(y) {}
            ~Calculator(void){ }
            const T add(void){ return _x + _y; }
            const T sub(void){ return _x -_y; }
            const T mult(void){ return _x * _y; }
            const T div(void){ return _x / _y; }
    
    private:
            const T _x;
            const T _y;
    };
    
    
    int main()
    {
      Calculator<blob> hello("a\n","b\n");
      return 0;
    }

  6. #6
    Senior Member oolon's Avatar
    Join Date
    Mar 2007
    Location
    London
    Posts
    2,294
    Thanks
    150
    Thanked
    302 times in 248 posts
    • oolon's system
      • Motherboard:
      • Asus P6T6
      • CPU:
      • Xeon w3680
      • Memory:
      • 3*4GB Kingston ECC
      • Storage:
      • 160GB Intel G2 SSD
      • Graphics card(s):
      • XFX HD6970 2GB
      • PSU:
      • Corsair HX850
      • Case:
      • Antec P183
      • Operating System:
      • Windows 7 Ultimate and Centos 5
      • Monitor(s):
      • Dell 2408WFP
      • Internet:
      • Be* Unlimied 6 down/1.2 up

    Re: Quick C++ question

    Quote Originally Posted by XA04 View Post
    The thing I don't understand is that there aren't any _x and _y classes? Or is that actually defining those classes right there and then? If so that makes sense!
    They are instances of the class that is passed to the template Calculator... see my example above. The technical term for the classes _X and _Y are arrogated classes of the template class. What that means they are instances of classes are used inside the class rather than extending them to provide new functionality.
    Last edited by oolon; 10-02-2010 at 04:17 PM. Reason: adding more info.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Just a quick question re DFI LP DK X48 T2R
    By spiderdany in forum SCAN.care@HEXUS
    Replies: 0
    Last Post: 19-08-2008, 06:37 PM
  2. Quick Question - Just placed an order...
    By fonz_valo in forum SCAN.care@HEXUS
    Replies: 3
    Last Post: 01-08-2008, 10:05 AM
  3. Just a quick question - Hard Drive
    By Crazy Fool in forum PC Hardware and Components
    Replies: 2
    Last Post: 13-09-2005, 03:20 PM
  4. Quick Question: PSU's with 1x120mm fan question
    By philyau in forum PC Hardware and Components
    Replies: 10
    Last Post: 05-09-2005, 02:30 PM
  5. Quick Linux question from a newbie...
    By madmonkey in forum Software
    Replies: 19
    Last Post: 26-08-2005, 09:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •