Object initialization in C++












1














I am looking at someone's code, I don't understand how the object getting initialized here:



template <typename String>
void test_numbers()
{
SampleClass<String> compare;
String lhs = "abc";
String rhs = "efg";
check_equality(compare(lhs, rhs), true);
}


The object compare is created of class type SampleClass and then assigned 2 strings when passed on as a parameter. How this initialization works? Any comments? suggestions?










share|improve this question
























  • Which initialization?
    – kennytm
    Feb 23 '12 at 19:32






  • 4




    That depends on the definition of SampleClass which you haven't shown. I suspect it is a function object which is poorly named or this is not the actual code you are looking at.
    – AJG85
    Feb 23 '12 at 19:33
















1














I am looking at someone's code, I don't understand how the object getting initialized here:



template <typename String>
void test_numbers()
{
SampleClass<String> compare;
String lhs = "abc";
String rhs = "efg";
check_equality(compare(lhs, rhs), true);
}


The object compare is created of class type SampleClass and then assigned 2 strings when passed on as a parameter. How this initialization works? Any comments? suggestions?










share|improve this question
























  • Which initialization?
    – kennytm
    Feb 23 '12 at 19:32






  • 4




    That depends on the definition of SampleClass which you haven't shown. I suspect it is a function object which is poorly named or this is not the actual code you are looking at.
    – AJG85
    Feb 23 '12 at 19:33














1












1








1







I am looking at someone's code, I don't understand how the object getting initialized here:



template <typename String>
void test_numbers()
{
SampleClass<String> compare;
String lhs = "abc";
String rhs = "efg";
check_equality(compare(lhs, rhs), true);
}


The object compare is created of class type SampleClass and then assigned 2 strings when passed on as a parameter. How this initialization works? Any comments? suggestions?










share|improve this question















I am looking at someone's code, I don't understand how the object getting initialized here:



template <typename String>
void test_numbers()
{
SampleClass<String> compare;
String lhs = "abc";
String rhs = "efg";
check_equality(compare(lhs, rhs), true);
}


The object compare is created of class type SampleClass and then assigned 2 strings when passed on as a parameter. How this initialization works? Any comments? suggestions?







c++ class templates object






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 2:40









Cœur

17.4k9102143




17.4k9102143










asked Feb 23 '12 at 19:29









RAB

409




409












  • Which initialization?
    – kennytm
    Feb 23 '12 at 19:32






  • 4




    That depends on the definition of SampleClass which you haven't shown. I suspect it is a function object which is poorly named or this is not the actual code you are looking at.
    – AJG85
    Feb 23 '12 at 19:33


















  • Which initialization?
    – kennytm
    Feb 23 '12 at 19:32






  • 4




    That depends on the definition of SampleClass which you haven't shown. I suspect it is a function object which is poorly named or this is not the actual code you are looking at.
    – AJG85
    Feb 23 '12 at 19:33
















Which initialization?
– kennytm
Feb 23 '12 at 19:32




Which initialization?
– kennytm
Feb 23 '12 at 19:32




4




4




That depends on the definition of SampleClass which you haven't shown. I suspect it is a function object which is poorly named or this is not the actual code you are looking at.
– AJG85
Feb 23 '12 at 19:33




That depends on the definition of SampleClass which you haven't shown. I suspect it is a function object which is poorly named or this is not the actual code you are looking at.
– AJG85
Feb 23 '12 at 19:33












2 Answers
2






active

oldest

votes


















4














//I am initialised with my default constructor (no args) 
SampleClass<String> compare;

//I am initialised with my `const char*` constructor (and assignment operator)
String lhs = "abc";
String rhs = "efg";

//Compare (already initialised) is being invoked by it's `operator()`
check_equality(compare(lhs, rhs), true);


compare is already constructed. It has an operator() implemented that allows it to appear as a function, accepting arguments.



you can make your own easily.



struct op_test{
int i;
op_test(int i_) : i(i_){}
int operator()(int j)const { return j*i; }
};
:::
op_test ot(5);
ot(6); //5*6


The reason this is useful is because we can do thing like this.



std::vector<int> a(700); //700 ints

std::transform(a.begin(), a.end(), b.begin(), op_test(5));
//or
std::transform(a.begin(), a.end(), b.begin(), &my_func); //calls a function
std::transform(a.begin(), a.end(), b.begin(), (int i){ return i*5; }); //lambda


see here:
http://msdn.microsoft.com/en-us/library/5tk49fh2(v=vs.80).aspx
useful with
http://en.cppreference.com/w/cpp/algorithm






share|improve this answer























  • "//Compare (already initialised) is being invoked by it's operator()" Can you please elaborate on this? thanks!
    – RAB
    Feb 23 '12 at 19:34












  • thank you very much, it solved my headache :)
    – RAB
    Feb 23 '12 at 19:39










  • I am glad I could be of service.
    – 111111
    Feb 23 '12 at 19:40



















0














It simply creates an automatic variable of type SampleClass<String>. Then its operator() is called with two String arguments.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9419853%2fobject-initialization-in-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    //I am initialised with my default constructor (no args) 
    SampleClass<String> compare;

    //I am initialised with my `const char*` constructor (and assignment operator)
    String lhs = "abc";
    String rhs = "efg";

    //Compare (already initialised) is being invoked by it's `operator()`
    check_equality(compare(lhs, rhs), true);


    compare is already constructed. It has an operator() implemented that allows it to appear as a function, accepting arguments.



    you can make your own easily.



    struct op_test{
    int i;
    op_test(int i_) : i(i_){}
    int operator()(int j)const { return j*i; }
    };
    :::
    op_test ot(5);
    ot(6); //5*6


    The reason this is useful is because we can do thing like this.



    std::vector<int> a(700); //700 ints

    std::transform(a.begin(), a.end(), b.begin(), op_test(5));
    //or
    std::transform(a.begin(), a.end(), b.begin(), &my_func); //calls a function
    std::transform(a.begin(), a.end(), b.begin(), (int i){ return i*5; }); //lambda


    see here:
    http://msdn.microsoft.com/en-us/library/5tk49fh2(v=vs.80).aspx
    useful with
    http://en.cppreference.com/w/cpp/algorithm






    share|improve this answer























    • "//Compare (already initialised) is being invoked by it's operator()" Can you please elaborate on this? thanks!
      – RAB
      Feb 23 '12 at 19:34












    • thank you very much, it solved my headache :)
      – RAB
      Feb 23 '12 at 19:39










    • I am glad I could be of service.
      – 111111
      Feb 23 '12 at 19:40
















    4














    //I am initialised with my default constructor (no args) 
    SampleClass<String> compare;

    //I am initialised with my `const char*` constructor (and assignment operator)
    String lhs = "abc";
    String rhs = "efg";

    //Compare (already initialised) is being invoked by it's `operator()`
    check_equality(compare(lhs, rhs), true);


    compare is already constructed. It has an operator() implemented that allows it to appear as a function, accepting arguments.



    you can make your own easily.



    struct op_test{
    int i;
    op_test(int i_) : i(i_){}
    int operator()(int j)const { return j*i; }
    };
    :::
    op_test ot(5);
    ot(6); //5*6


    The reason this is useful is because we can do thing like this.



    std::vector<int> a(700); //700 ints

    std::transform(a.begin(), a.end(), b.begin(), op_test(5));
    //or
    std::transform(a.begin(), a.end(), b.begin(), &my_func); //calls a function
    std::transform(a.begin(), a.end(), b.begin(), (int i){ return i*5; }); //lambda


    see here:
    http://msdn.microsoft.com/en-us/library/5tk49fh2(v=vs.80).aspx
    useful with
    http://en.cppreference.com/w/cpp/algorithm






    share|improve this answer























    • "//Compare (already initialised) is being invoked by it's operator()" Can you please elaborate on this? thanks!
      – RAB
      Feb 23 '12 at 19:34












    • thank you very much, it solved my headache :)
      – RAB
      Feb 23 '12 at 19:39










    • I am glad I could be of service.
      – 111111
      Feb 23 '12 at 19:40














    4












    4








    4






    //I am initialised with my default constructor (no args) 
    SampleClass<String> compare;

    //I am initialised with my `const char*` constructor (and assignment operator)
    String lhs = "abc";
    String rhs = "efg";

    //Compare (already initialised) is being invoked by it's `operator()`
    check_equality(compare(lhs, rhs), true);


    compare is already constructed. It has an operator() implemented that allows it to appear as a function, accepting arguments.



    you can make your own easily.



    struct op_test{
    int i;
    op_test(int i_) : i(i_){}
    int operator()(int j)const { return j*i; }
    };
    :::
    op_test ot(5);
    ot(6); //5*6


    The reason this is useful is because we can do thing like this.



    std::vector<int> a(700); //700 ints

    std::transform(a.begin(), a.end(), b.begin(), op_test(5));
    //or
    std::transform(a.begin(), a.end(), b.begin(), &my_func); //calls a function
    std::transform(a.begin(), a.end(), b.begin(), (int i){ return i*5; }); //lambda


    see here:
    http://msdn.microsoft.com/en-us/library/5tk49fh2(v=vs.80).aspx
    useful with
    http://en.cppreference.com/w/cpp/algorithm






    share|improve this answer














    //I am initialised with my default constructor (no args) 
    SampleClass<String> compare;

    //I am initialised with my `const char*` constructor (and assignment operator)
    String lhs = "abc";
    String rhs = "efg";

    //Compare (already initialised) is being invoked by it's `operator()`
    check_equality(compare(lhs, rhs), true);


    compare is already constructed. It has an operator() implemented that allows it to appear as a function, accepting arguments.



    you can make your own easily.



    struct op_test{
    int i;
    op_test(int i_) : i(i_){}
    int operator()(int j)const { return j*i; }
    };
    :::
    op_test ot(5);
    ot(6); //5*6


    The reason this is useful is because we can do thing like this.



    std::vector<int> a(700); //700 ints

    std::transform(a.begin(), a.end(), b.begin(), op_test(5));
    //or
    std::transform(a.begin(), a.end(), b.begin(), &my_func); //calls a function
    std::transform(a.begin(), a.end(), b.begin(), (int i){ return i*5; }); //lambda


    see here:
    http://msdn.microsoft.com/en-us/library/5tk49fh2(v=vs.80).aspx
    useful with
    http://en.cppreference.com/w/cpp/algorithm







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 23 '12 at 19:37

























    answered Feb 23 '12 at 19:32









    111111

    11.8k43153




    11.8k43153












    • "//Compare (already initialised) is being invoked by it's operator()" Can you please elaborate on this? thanks!
      – RAB
      Feb 23 '12 at 19:34












    • thank you very much, it solved my headache :)
      – RAB
      Feb 23 '12 at 19:39










    • I am glad I could be of service.
      – 111111
      Feb 23 '12 at 19:40


















    • "//Compare (already initialised) is being invoked by it's operator()" Can you please elaborate on this? thanks!
      – RAB
      Feb 23 '12 at 19:34












    • thank you very much, it solved my headache :)
      – RAB
      Feb 23 '12 at 19:39










    • I am glad I could be of service.
      – 111111
      Feb 23 '12 at 19:40
















    "//Compare (already initialised) is being invoked by it's operator()" Can you please elaborate on this? thanks!
    – RAB
    Feb 23 '12 at 19:34






    "//Compare (already initialised) is being invoked by it's operator()" Can you please elaborate on this? thanks!
    – RAB
    Feb 23 '12 at 19:34














    thank you very much, it solved my headache :)
    – RAB
    Feb 23 '12 at 19:39




    thank you very much, it solved my headache :)
    – RAB
    Feb 23 '12 at 19:39












    I am glad I could be of service.
    – 111111
    Feb 23 '12 at 19:40




    I am glad I could be of service.
    – 111111
    Feb 23 '12 at 19:40













    0














    It simply creates an automatic variable of type SampleClass<String>. Then its operator() is called with two String arguments.






    share|improve this answer


























      0














      It simply creates an automatic variable of type SampleClass<String>. Then its operator() is called with two String arguments.






      share|improve this answer
























        0












        0








        0






        It simply creates an automatic variable of type SampleClass<String>. Then its operator() is called with two String arguments.






        share|improve this answer












        It simply creates an automatic variable of type SampleClass<String>. Then its operator() is called with two String arguments.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 23 '12 at 19:33









        Cat Plus Plus

        89.8k22166201




        89.8k22166201






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9419853%2fobject-initialization-in-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python