throw assignment operator












-2














C++



How do I throw an error when someone calls the assignment operator?



I have a base class that uses a factory method instead of a constructor. The factory methods reads a file and calls the derived class constructor based on the file contents.



Base *a::create(file1);
Base *b::create(file2);


I want to throw an error if someone calls the assignment operator.



*a = *b; // filetype contents don't match


At this moment when the above is executed, my *a's contents are overwritten by *b. I'm assuming its calling the implicit assignment operator, which is what I don't want to happen.



When I declare the assignment operator private. I get the following errors when I run it in a separate test file.



test.cc:34:13: fatal error: 'operator=' is a private member of 'Test'
*aa = *ad;
~~~ ^ ~~~
./Base.h:14:16: note: declared private here
Base& operator=(const Base &rhs);
^
1 error generated.
make: *** [test] Error 1


I would like it to rather throw an error instead of what is shown above. Like "you can't do this" or something.



Any help would be appreciated.










share|improve this question
























  • Have you tried making it public and using throw?
    – zneak
    Nov 11 at 3:08






  • 1




    I would like it to rather throw an error instead of what is shown above. Like "you can't do this" or something. Isn't this exactly what you're getting right now? The compiler is telling you You can't do this. Alternatively you can = delete the function, the error would be different, the result would be the same.
    – tkausl
    Nov 11 at 3:08










  • I'm told it has to display an error message and the assignment operator can't be public.
    – N. Colostate
    Nov 11 at 3:11












  • It does display an error message. What is your problem?
    – Aganju
    Nov 11 at 3:15










  • I was docked points for not throwing an error. It displayed what is shown above. I'm supposed to throw an error and the method can't be public.
    – N. Colostate
    Nov 11 at 3:16
















-2














C++



How do I throw an error when someone calls the assignment operator?



I have a base class that uses a factory method instead of a constructor. The factory methods reads a file and calls the derived class constructor based on the file contents.



Base *a::create(file1);
Base *b::create(file2);


I want to throw an error if someone calls the assignment operator.



*a = *b; // filetype contents don't match


At this moment when the above is executed, my *a's contents are overwritten by *b. I'm assuming its calling the implicit assignment operator, which is what I don't want to happen.



When I declare the assignment operator private. I get the following errors when I run it in a separate test file.



test.cc:34:13: fatal error: 'operator=' is a private member of 'Test'
*aa = *ad;
~~~ ^ ~~~
./Base.h:14:16: note: declared private here
Base& operator=(const Base &rhs);
^
1 error generated.
make: *** [test] Error 1


I would like it to rather throw an error instead of what is shown above. Like "you can't do this" or something.



Any help would be appreciated.










share|improve this question
























  • Have you tried making it public and using throw?
    – zneak
    Nov 11 at 3:08






  • 1




    I would like it to rather throw an error instead of what is shown above. Like "you can't do this" or something. Isn't this exactly what you're getting right now? The compiler is telling you You can't do this. Alternatively you can = delete the function, the error would be different, the result would be the same.
    – tkausl
    Nov 11 at 3:08










  • I'm told it has to display an error message and the assignment operator can't be public.
    – N. Colostate
    Nov 11 at 3:11












  • It does display an error message. What is your problem?
    – Aganju
    Nov 11 at 3:15










  • I was docked points for not throwing an error. It displayed what is shown above. I'm supposed to throw an error and the method can't be public.
    – N. Colostate
    Nov 11 at 3:16














-2












-2








-2







C++



How do I throw an error when someone calls the assignment operator?



I have a base class that uses a factory method instead of a constructor. The factory methods reads a file and calls the derived class constructor based on the file contents.



Base *a::create(file1);
Base *b::create(file2);


I want to throw an error if someone calls the assignment operator.



*a = *b; // filetype contents don't match


At this moment when the above is executed, my *a's contents are overwritten by *b. I'm assuming its calling the implicit assignment operator, which is what I don't want to happen.



When I declare the assignment operator private. I get the following errors when I run it in a separate test file.



test.cc:34:13: fatal error: 'operator=' is a private member of 'Test'
*aa = *ad;
~~~ ^ ~~~
./Base.h:14:16: note: declared private here
Base& operator=(const Base &rhs);
^
1 error generated.
make: *** [test] Error 1


I would like it to rather throw an error instead of what is shown above. Like "you can't do this" or something.



Any help would be appreciated.










share|improve this question















C++



How do I throw an error when someone calls the assignment operator?



I have a base class that uses a factory method instead of a constructor. The factory methods reads a file and calls the derived class constructor based on the file contents.



Base *a::create(file1);
Base *b::create(file2);


I want to throw an error if someone calls the assignment operator.



*a = *b; // filetype contents don't match


At this moment when the above is executed, my *a's contents are overwritten by *b. I'm assuming its calling the implicit assignment operator, which is what I don't want to happen.



When I declare the assignment operator private. I get the following errors when I run it in a separate test file.



test.cc:34:13: fatal error: 'operator=' is a private member of 'Test'
*aa = *ad;
~~~ ^ ~~~
./Base.h:14:16: note: declared private here
Base& operator=(const Base &rhs);
^
1 error generated.
make: *** [test] Error 1


I would like it to rather throw an error instead of what is shown above. Like "you can't do this" or something.



Any help would be appreciated.







c++ assignment-operator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 3:09

























asked Nov 11 at 3:00









N. Colostate

356




356












  • Have you tried making it public and using throw?
    – zneak
    Nov 11 at 3:08






  • 1




    I would like it to rather throw an error instead of what is shown above. Like "you can't do this" or something. Isn't this exactly what you're getting right now? The compiler is telling you You can't do this. Alternatively you can = delete the function, the error would be different, the result would be the same.
    – tkausl
    Nov 11 at 3:08










  • I'm told it has to display an error message and the assignment operator can't be public.
    – N. Colostate
    Nov 11 at 3:11












  • It does display an error message. What is your problem?
    – Aganju
    Nov 11 at 3:15










  • I was docked points for not throwing an error. It displayed what is shown above. I'm supposed to throw an error and the method can't be public.
    – N. Colostate
    Nov 11 at 3:16


















  • Have you tried making it public and using throw?
    – zneak
    Nov 11 at 3:08






  • 1




    I would like it to rather throw an error instead of what is shown above. Like "you can't do this" or something. Isn't this exactly what you're getting right now? The compiler is telling you You can't do this. Alternatively you can = delete the function, the error would be different, the result would be the same.
    – tkausl
    Nov 11 at 3:08










  • I'm told it has to display an error message and the assignment operator can't be public.
    – N. Colostate
    Nov 11 at 3:11












  • It does display an error message. What is your problem?
    – Aganju
    Nov 11 at 3:15










  • I was docked points for not throwing an error. It displayed what is shown above. I'm supposed to throw an error and the method can't be public.
    – N. Colostate
    Nov 11 at 3:16
















Have you tried making it public and using throw?
– zneak
Nov 11 at 3:08




Have you tried making it public and using throw?
– zneak
Nov 11 at 3:08




1




1




I would like it to rather throw an error instead of what is shown above. Like "you can't do this" or something. Isn't this exactly what you're getting right now? The compiler is telling you You can't do this. Alternatively you can = delete the function, the error would be different, the result would be the same.
– tkausl
Nov 11 at 3:08




I would like it to rather throw an error instead of what is shown above. Like "you can't do this" or something. Isn't this exactly what you're getting right now? The compiler is telling you You can't do this. Alternatively you can = delete the function, the error would be different, the result would be the same.
– tkausl
Nov 11 at 3:08












I'm told it has to display an error message and the assignment operator can't be public.
– N. Colostate
Nov 11 at 3:11






I'm told it has to display an error message and the assignment operator can't be public.
– N. Colostate
Nov 11 at 3:11














It does display an error message. What is your problem?
– Aganju
Nov 11 at 3:15




It does display an error message. What is your problem?
– Aganju
Nov 11 at 3:15












I was docked points for not throwing an error. It displayed what is shown above. I'm supposed to throw an error and the method can't be public.
– N. Colostate
Nov 11 at 3:16




I was docked points for not throwing an error. It displayed what is shown above. I'm supposed to throw an error and the method can't be public.
– N. Colostate
Nov 11 at 3:16












1 Answer
1






active

oldest

votes


















2














Compile time errors are better than runtime ones (because they prevent the developer from thinking their code might work when it actually has no chance). The right thing to do is declare the unusable operators and constructors as private or protected, so that they cannot be used except by friends (e.g. factories).






share|improve this answer





















  • Can you not delete the assignment operator(s) in modern C++?
    – Jonathan Leffler
    Nov 11 at 3:27










  • @JonathanLeffler: Sure, you can = delete the operators, in which case it doesn't matter if they're public or not. Good point, assuming the functions aren't needed by a factory.
    – John Zwinck
    Nov 11 at 3:33













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%2f53245480%2fthrow-assignment-operator%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Compile time errors are better than runtime ones (because they prevent the developer from thinking their code might work when it actually has no chance). The right thing to do is declare the unusable operators and constructors as private or protected, so that they cannot be used except by friends (e.g. factories).






share|improve this answer





















  • Can you not delete the assignment operator(s) in modern C++?
    – Jonathan Leffler
    Nov 11 at 3:27










  • @JonathanLeffler: Sure, you can = delete the operators, in which case it doesn't matter if they're public or not. Good point, assuming the functions aren't needed by a factory.
    – John Zwinck
    Nov 11 at 3:33


















2














Compile time errors are better than runtime ones (because they prevent the developer from thinking their code might work when it actually has no chance). The right thing to do is declare the unusable operators and constructors as private or protected, so that they cannot be used except by friends (e.g. factories).






share|improve this answer





















  • Can you not delete the assignment operator(s) in modern C++?
    – Jonathan Leffler
    Nov 11 at 3:27










  • @JonathanLeffler: Sure, you can = delete the operators, in which case it doesn't matter if they're public or not. Good point, assuming the functions aren't needed by a factory.
    – John Zwinck
    Nov 11 at 3:33
















2












2








2






Compile time errors are better than runtime ones (because they prevent the developer from thinking their code might work when it actually has no chance). The right thing to do is declare the unusable operators and constructors as private or protected, so that they cannot be used except by friends (e.g. factories).






share|improve this answer












Compile time errors are better than runtime ones (because they prevent the developer from thinking their code might work when it actually has no chance). The right thing to do is declare the unusable operators and constructors as private or protected, so that they cannot be used except by friends (e.g. factories).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 3:08









John Zwinck

150k16175286




150k16175286












  • Can you not delete the assignment operator(s) in modern C++?
    – Jonathan Leffler
    Nov 11 at 3:27










  • @JonathanLeffler: Sure, you can = delete the operators, in which case it doesn't matter if they're public or not. Good point, assuming the functions aren't needed by a factory.
    – John Zwinck
    Nov 11 at 3:33




















  • Can you not delete the assignment operator(s) in modern C++?
    – Jonathan Leffler
    Nov 11 at 3:27










  • @JonathanLeffler: Sure, you can = delete the operators, in which case it doesn't matter if they're public or not. Good point, assuming the functions aren't needed by a factory.
    – John Zwinck
    Nov 11 at 3:33


















Can you not delete the assignment operator(s) in modern C++?
– Jonathan Leffler
Nov 11 at 3:27




Can you not delete the assignment operator(s) in modern C++?
– Jonathan Leffler
Nov 11 at 3:27












@JonathanLeffler: Sure, you can = delete the operators, in which case it doesn't matter if they're public or not. Good point, assuming the functions aren't needed by a factory.
– John Zwinck
Nov 11 at 3:33






@JonathanLeffler: Sure, you can = delete the operators, in which case it doesn't matter if they're public or not. Good point, assuming the functions aren't needed by a factory.
– John Zwinck
Nov 11 at 3:33




















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%2f53245480%2fthrow-assignment-operator%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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini