throw assignment operator
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
|
show 1 more comment
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
Have you tried making it public and usingthrow
?
– 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 youYou 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
|
show 1 more comment
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
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
c++ assignment-operator
edited Nov 11 at 3:09
asked Nov 11 at 3:00
N. Colostate
356
356
Have you tried making it public and usingthrow
?
– 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 youYou 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
|
show 1 more comment
Have you tried making it public and usingthrow
?
– 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 youYou 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
|
show 1 more comment
1 Answer
1
active
oldest
votes
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 friend
s (e.g. factories).
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'republic
or not. Good point, assuming the functions aren't needed by a factory.
– John Zwinck
Nov 11 at 3:33
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 friend
s (e.g. factories).
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'republic
or not. Good point, assuming the functions aren't needed by a factory.
– John Zwinck
Nov 11 at 3:33
add a comment |
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 friend
s (e.g. factories).
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'republic
or not. Good point, assuming the functions aren't needed by a factory.
– John Zwinck
Nov 11 at 3:33
add a comment |
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 friend
s (e.g. factories).
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 friend
s (e.g. factories).
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'republic
or not. Good point, assuming the functions aren't needed by a factory.
– John Zwinck
Nov 11 at 3:33
add a comment |
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'republic
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 youYou 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