我有一个工厂方法可以返回类类型的对象。
MyClass factory(string param1, ...) {
...
MyClass instance(param1);
...
if (smth) throw ...;
...
return instance;
}
工厂方法可能会抛出。
我正在尝试使用异常处理创建此类的对象。像这样:
MyClass obj;
try {
obj = factory(...);
} catch (...) {
cout << "oops..." << endl;
}
当然,该问题不像MyClass obj
创建实例那样起作用,但是我想避免创建实例,因为这很浪费时间和资源。
可能我可以将factory
的返回类型更改为MyClass*
,但是然后我需要面对new
和delete
。
有没有一种方法可以在没有new
的情况下创建临时实例,但要进行异常处理?
答案 0 :(得分:2)
怎么样:
@ECHO ON
::MODE con: cols=100 lines=50
::Custom Data Copying
::Copy Chrome Bookmarks
TASKKILL /f /IM Chrome.exe
MKDIR "C:\Users\%username%\AppData\Local\Google\Chrome\User Data\Default"
DEL /F /Q "C:\Users\%username%\AppData\Local\Google\Chrome\User Data\Default\Bookmarks"
DEL /F /Q "C:\Users\%username%\AppData\Local\Google\Chrome\User Data\Default\Bookmarks.bak"
XCOPY /S /H /Y /I "..\..\%computername%\Chrome_Bookmarks" "C:\Users\%username%\AppData\Local\Google\Chrome\User Data\Default"
::Copy and Rename Sticky Notes
MKDIR "C:\Users\%username%\Appdata\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy"
XCOPY /S /H /Y /I "..\..\%computername%\Sticky_Notes" "C:\Users\%username%\Appdata\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy"
REN "C:\Users\%username%\Appdata\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy\StickyNotes.snt" ThresholdNotes.snt
::Copy Outlook Signature
MKDIR "C:\Users\%username%\AppData\Roaming\Microsoft\Signatures"
XCOPY /S /H /Y /I "..\..\%computername%\Outlook_Signature" "C:\Users\%username%\AppData\Roaming\Microsoft\Signatures"
::Installed Programs List
XCOPY /S /H /Y /I "..\..\%computername%\Installed_Programs\%username%_ProgramsList.txt" %Userprofile%\Desktop
Start "" "C:\Users\%username%\Desktop\%username%_ProgramsList.txt"
::Import Network Drives and List
Regedit.exe /S "..\..\%computername%\Network_Drives\%username%drives.reg\%username%drives.reg"
::Copy Local Ad. Account Pass. Reset
XCOPY /S /H /Y /I "AdminPass.bat" "C:\Users\Public\Desktop"
::Import Wireless Connections
FORFILES /P "..\..\%computername%\Wireless_Networks" /M *.xml /C "cmd /C netsh wlan add profile @path user=all"
::WEB LINKS FOLDERS
C:
CD C:\Users\%username%\Desktop
RMDIR /s /q "Web_Links"
MKDIR "C:\Users\%username%\Desktop\Web Links"
XCOPY /S /H /Y /I "\\server\server\server\Web_Links\Win_10" "C:\Users\%username%\Desktop\Web Links"
::Data Collection for Logs
CLS
@ECHO ON
ECHO Current Computer Name is...
ECHO %Computername%
ECHO.
SET /P Adminuser=What is your admin username?
PUSHD %~dp0
ECHO %computername%, %date%, %time%, %Adminuser%, %username% >> "..\_Logs\Computer_Names\New_Computers\New_Computers.csv"
TIMEOUT 2
POPD
C:
CD C:\Users\%username%\Desktop
GOTO REGFIX
答案 1 :(得分:2)
您可以使用std::optional
(自C ++ 17起)。
(重点是我的)
类模板
std::optional
管理一个可选的包含 值...如果
optional<T>
包含一个值,则保证该值为 分配为可选对象足迹的一部分,即无动态 内存分配曾经发生过。因此,可选对象为 对象,而不是指针,即使operator *()和operator->()是 定义。
例如
std::optional<MyClass> obj;
try {
obj = factory(...);
} catch (...) {
cout << "oops..." << endl;
}
顺便说一句:如果您只是不想手动处理new
和delete
,那么smart pointers也是不错的选择。