dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 32

Thread: using namespace std

  1. #16
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,115

    Re: using namespace std

    I haven't had time to read everything yet, but I see you are using NULL.
    In modern C++, you should not use NULL anymore.
    Instead, you should really start using nullptr.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  2. #17
    Join Date
    May 2018
    Posts
    67

    Re: using namespace std

    Quote Originally Posted by Marc G View Post
    Instead, you should really start using nullptr.
    Thank you for your warning.
    As I said this code is published by italian university in exam session hence I should follow this method if I want pass, I didn't write this code.

  3. #18
    Join Date
    May 2018
    Posts
    67

    Re: using namespace std

    Another thing which I'm not able to understand in this code.
    I note in similar code that sometimes there is masking of copy/assignment constructor.
    I know that with dynamic data It's necessary to custom copy/assignment constructor to allocate/deallocate when It's necessary.
    But if these constructors are not used in this code it's better to mask them in private section to avoid its use:

    Code:
    ...
    private:
       Seggio(const Seggio&);    //copy constructor
       Seggio& operator=(const Seggio&);     //assignment constructor
    ...
    In this way if in code someone wrote assignment, compiler should give error.

    Leave it to the experts
    Last edited by zio_mangrovia; June 10th, 2018 at 02:20 AM.

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

    Re: using namespace std

    That is the 'old' way of dis-allowing the object to be copied. The modern way is

    Code:
    public:
    Seggio(const Seggio&) = delete;
    Seggio& operator=(const Seggio&) = delete;
    Classes that use dynamic memory require a 'deep copy' (ie allocation of memory for the new object and all the contents copied from source to destination). By default, c++ does a 'shallow' copy - just copies the pointer so that the new and old object both use the same memory with predictable fatal results. Hence if proper 'deep copy' methods are not provided then these should be disabled. Prior to c++11, putting these in the private section of the class was the way to do this, but since c++11 there has been =delete which is used.

    From what I've seen of the code you are being taught c++98 and not the latest c++17 standard. There has been massive changes to the c++ language since the original c++98 standard.
    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++17 Compiler: Microsoft VS2017 (15.8.7)

  5. #20
    Join Date
    May 2018
    Posts
    67

    Re: using namespace std

    Quote Originally Posted by 2kaud View Post
    That is the 'old' way of dis-allowing the object to be copied.
    ... Hence if proper 'deep copy' methods are not provided then these should be disabled. ...
    Regardless old/modern way, do you think in this code it's neccessary, right? Because here it has not been written while in other example it's written.
    Now I'm studying only concepts.

    thanks again.

  6. #21
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,115

    Re: using namespace std

    If you have a class with dynamic memory, and you don't want to implement a copy constructor and copy assignment operator, then you should explicitly delete them.
    If you don't explicitly delete them, the compiler will implicitly create them for you, but they will do a shallow copy, which will cause your program to behave strangely or even crash.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

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

    Re: using namespace std

    You could always code the copy constructor as an exercise. And if you do, also code a class swap function to swap the contents of two Schedario objects. Once you have these (together with the existing class destructor) then writing operator= is trivial. Also in modern c++ there are additionally move constructor and move assignment. But once you have swap(), again these are trivial to implement.
    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++17 Compiler: Microsoft VS2017 (15.8.7)

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

    Re: using namespace std

    Why VictorN has written include directives different from yours?
    Ask 10 experienced c++ programmers to write a program and you'll probably get 10 different programs that look different but perform the same task.... Once we become experienced, we tend to develop our own little idiosyncrasies that put our own 'personality' in the code. If you look in the various codeguru threads for code written by Victor, myself and others then look at another sample of code, you'll probably be able to tell you wrote it. That's one of the reasons why programming teams tend to produce and use 'coding guidelines' so that everyone in the team adopts a certain 'house style'.
    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++17 Compiler: Microsoft VS2017 (15.8.7)

  9. #24
    Join Date
    May 2018
    Posts
    67

    Re: using namespace std

    Quote Originally Posted by Marc G View Post
    If you have a class with dynamic memory, and you don't want to implement a copy constructor and copy assignment operator, then you should explicitly delete them.
    In this code I don't want to implement a copy constructor and copy assignment operator but to be sure programmer doesn't use it I can mask it into private as I said in previous messages?
    What do you think about copy/assignment constructor of this code please ?

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

    Re: using namespace std

    The 'correct' modern c++ way to not allow copy constructor/operator = is

    Code:
    public:
    	Schedario(const Schedario&) = delete;
    	Schedario& operator=(const Schedario&) = delete;
    What do you think about copy/assignment constructor of this code please ?
    Writing a copy constructor for this class is not that difficult. Iterate the class to be copied from and insert each element into the new class - watch the ordering, if you're not careful the copy will be the reverse of the original! As I said in post #22, also implement a class swap function. operator= is then trivial.
    Last edited by 2kaud; June 10th, 2018 at 08:20 AM.
    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++17 Compiler: Microsoft VS2017 (15.8.7)

  11. #26
    Join Date
    May 2018
    Posts
    67

    Re: using namespace std

    Quote Originally Posted by 2kaud View Post
    The 'correct' modern c++ way to not allow copy constructor/operator = is

    Code:
    public:
    	Schedario(const Schedario&) = delete;
    	Schedario& operator=(const Schedario&) = delete;


    Writing a copy constructor for this class is not that difficult.
    The point of view is different, problem is not how much was easy or difficult to implement copy constructor/assignment operator.
    In exam session I have to write both compito.h and compito.cpp while main.cpp is just written and I can't change it, so I'm trying to understand when to mask copy constructor/assignment operator with this code example.
    So I ask myself if in this case I had to implement it or no, I think yes because it's never called = operator and copy constructor in main.c
    What do you think?
    Last edited by zio_mangrovia; June 11th, 2018 at 05:34 AM.

  12. #27
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,115

    Re: using namespace std

    It really simple.
    Have a close look at the main.cpp.
    Is it doing any copying or copy assignments?
    If yes, implement them.
    If no, explicitly delete them (= delete).
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  13. #28
    Join Date
    May 2018
    Posts
    67

    Re: using namespace std

    Quote Originally Posted by Marc G View Post
    It really simple.
    Have a close look at the main.cpp.
    Is it doing any copying or copy assignments?
    If yes, implement them.
    If no, explicitly delete them (= delete).
    In this example, if I followed ''old way of dis-allowing the object to be copied", I should add in private side of class:


    Code:
       Seggio(const Seggio&);                      //copy constructor
       Seggio& operator=(const Seggio&);     //assignment constructor

    can you confirm me please?

  14. #29
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,115

    Re: using namespace std

    Yes, if you really want to keep using the old, legacy, non-recommended way, then that's correct.
    But I'm sorry, it's 2018, you should use the modern C++ "= delete" syntax.
    If I would ask the question, you would lose points if you'd use the old syntax.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

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

    Re: using namespace std

    When writing a class that uses dynamic memory, then you provide as a minimum

    - default constructor
    - other constructor(s) as required
    - destructor
    - copy constructor
    - copy assignment
    - move constructor
    - move assignment

    - swap() although not absolutely essential but it's used in the implementation of some of the above (eg copy assignment) so should be provided.

    unless there is a good reason why some of these should not be provided (eg unique_memory doesn't allow copying, just move for obvious reasons).

    So when asked to write a class that uses dynamic memory, then the above should be provided unless explicitly asked not to or not appropriate for the class. If this is an exam, then in the absence of any other instruction, assume that all of the above are required.
    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++17 Compiler: Microsoft VS2017 (15.8.7)

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)