CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Templates

  1. #1
    Join Date
    Jun 2019
    Posts
    30

    Templates

    Code:
    template<int,class T>
    class MyClass {
    public:
        T buff[10];
        T TestFunction(T val) {return buff[val];}
    };
    
    int main()
    {
        struct Mystruct {
            int val1;int val2
        };
    
        Myclass<10,Mystruct> myObj;
    }
    How to assign the data to val1 and val2 and how to access and pass as argument(struct) to Testfunction?
    Last edited by 2kaud; July 4th, 2019 at 03:21 AM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Templates

    Apart from typing errors, the code in post #1 does not initialise the array buff. Consider:

    Code:
    template<int SZ, class T>
    class MyClass {
    public:
    	MyClass() {}
    	T buff[SZ] {{1,2}, {3, 4}, {5, 6}};
    	T TestFunction(int val) { return buff[val]; }
    };
    
    int main()
    {
    	struct Mystruct {
    		int val1; int val2;
    	};
    
    	MyClass<10, Mystruct> myObj;
    
    	auto c = myObj.TestFunction(1);
    
    	cout << c.val1 << " " << c.val2 << endl;
    }
    Note that this test code initializes the contents of buff in the templated class assuming knowledge of type T. You'd probably want a constructor for MyStruct and have a constructor or member function for MyClass for buff initialisation.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2019
    Posts
    30

    Re: Templates

    Quote Originally Posted by 2kaud View Post
    Apart from typing errors, the code in post #1 does not initialise the array buff. Consider:

    Code:
    template<int SZ, class T>
    class MyClass {
    public:
    	MyClass() {}
    	T buff[SZ] {{1,2}, {3, 4}, {5, 6}};
    	T TestFunction(int val) { return buff[val]; }
    };
    
    int main()
    {
    	struct Mystruct {
    		int val1; int val2;
    	};
    
    	MyClass<10, Mystruct> myObj;
    
    	auto c = myObj.TestFunction(1);
    
    	cout << c.val1 << " " << c.val2 << endl;
    }
    Note that this test code initializes the contents of buff in the templated class assuming knowledge of type T. You'd probably want a constructor for MyStruct and have a constructor or member function for MyClass for buff initialisation.
    Ya i got your point.but if i have the following code then how to access the following functions

    Code:
    template<int size,class T>
    class MyClass {
    public:
            MyClass(const T &val)//parameterized constructor
            { globalVal = val;}
            T buff[10];
        void TestFunction(T val) { buff[index] = val;index++}//assume index will be reset in some other function
        T operator[](int localvalue){return buff[localvalue];}
    private:
      int globalval;
    };
    
    int main()
    {
        struct Mystruct {
            int val1;int val2
        };
    
        Myclass<10,Mystruct> myObj(Mystruct&);//so in this fucntion how to call operator[] and Testfunctions using  myobject and how to assign the data to val1 and val2?
    }

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Templates

    That code has some issues. Have you tried compiling it?

    In MyClass, val is a const ref to type T, but is assigned to a type int.

    You can't pass Mystruct as a myobj initialisation value, as Mystruct is a type.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jun 2019
    Posts
    30

    Re: Templates

    Quote Originally Posted by 2kaud View Post
    That code has some issues. Have you tried compiling it?

    In MyClass, val is a const ref to type T, but is assigned to a type int.

    You can't pass Mystruct as a myobj initialisation value, as Mystruct is a type.
    There is no compiling issues with the code.. as i tried before posting.

    straight forward query.
    Myclass<10,Mystruct> myObj();--> what should i pass as argument ,if i want to access the structure?

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Templates

    Code:
    int main()
    {
        struct Mystruct {
            int val1;int val2
        };
    
        Mystruct mys;
    
        Myclass<10,Mystruct> myObj(mys);
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Jun 2019
    Posts
    30

    Re: Templates

    How can we add parameterised Constructor to the Structure, for Example:

    Code:
    MyClass
    {
    };
    struct Mystruct
    {
    int val1;
    int val2;
     Myclass(/*to be added here*/);
    }
    Need to assign values to the val1 and val2?

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Templates

    Quote Originally Posted by Raj90 View Post
    How can we add parameterised Constructor to the Structure, for Example:

    Code:
    MyClass
    {
    };
    struct Mystruct
    {
      int val1;
      int val2;
      Myclass(/*to be added here*/);
    }
    Need to assign values to the val1 and val2?
    "parameterised Constructor" for the struct Mystruct or for MyClass?
    And BTW, what is MyClass?
    Victor Nijegorodov

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Templates

    Quote Originally Posted by Raj90 View Post
    How can we add parameterised Constructor to the Structure, for Example:

    Code:
    MyClass
    {
    };
    struct Mystruct
    {
    int val1;
    int val2;
     Myclass(/*to be added here*/);
    }
    Need to assign values to the val1 and val2?
    You add a constructor to the class/struct that takes the values you want to assign. Eg:

    Code:
    struct MyStruct
    {
    	int val1;
    	int val2;
    
    	MyStruct(int v1, int v2) : val1(v1), val2(v2) {}
    };
    
    
    int main()
    {
    	MyStruct mys(3, 4);
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured